0

I try to create a pattern to extract some values from a line but without success. May I kindly ask you to give some help?

I would like to extract values from a line like this (just an easy example):

502,42 421,421.421 421421,421.55 551
[0-9,.]+ 

The problem is that I would like to assign each and every value to a different variable.

How should I do that?

Timisorean
  • 1,388
  • 7
  • 20
  • 30
neptun
  • 15
  • 5
  • `^(?:\d+[,. ]){N-1}(\d+)`? (Where `N` is the position of the element) See: https://regex101.com/r/I4mEzR/1 – 41686d6564 stands w. Palestine Jul 22 '19 at 07:52
  • Dear Ahmed, Thank you for your answer in this regard. I have checked your solution but the goal would be to "split" the values on each Space \s, then I would have 4 different values and with the {N of element}- i can assign the value to a variable one by one by changing the N. is it possible? thanks, agian! – neptun Jul 22 '19 at 08:06
  • Are you using .NET? If so, why don't you use the [`Split()`](https://learn.microsoft.com/en-us/dotnet/api/system.string.split) method instead of regex? Anyway, with regex, you can do something like `^(?:[0-9,.]+\s){N-1}([0-9,.]+)` and your Nth element will be in the first capturing group. See: https://regex101.com/r/WVkcrH/1 Or you can use `^(?:[0-9,.]+\s){2}\K[0-9,.]+` if your regex flavor supports `\K`. – 41686d6564 stands w. Palestine Jul 22 '19 at 08:10
  • Possible duplicate of [regular expression capture nth match](https://stackoverflow.com/questions/23555593/regular-expression-capture-nth-match) – 41686d6564 stands w. Palestine Jul 22 '19 at 08:23

2 Answers2

0

Since you tagged this with UiPath, here's one approach. Create one variable holding your regex, and up to n variables for each match. Add an Invoke Code activity. Map input and output arguments to your variables (in my case there's one input - the regex, and two outputs).

regex in uipath

Change the code as required. The pattern might become another input argument.

Dim matches As MatchCollection = Regex.Matches(in_RegEx, "\d")
out_part0 = matches(0).Value
out_part1 = matches(1).Value

Assuming you provided 1 2 3 4 as input and \d as the pattern, part0 == 1 and part1 == 2.

Wolfgang Radl
  • 2,319
  • 2
  • 17
  • 22
0

I'm very glad to see your comments, thank you for your cooperation in this regard. I also found a solution for my "problem":

  • Read PDF Text (or other source...)
  • Matches activity - with the pattern
  • Assign activity (as many as needed - for me the Variable type is Double) = Double.Parse((Matches(0).Groups(2).value).ToString.Replace(".","").Replace(",",".").Trim)
  • Write line (just for checking the results)

If you create a pattern, the only thing which should be changed is the number of the .Groups(N) within the Assign activity.

neptun

neptun
  • 15
  • 5