agnesv
(Agnes)
February 19, 2020, 1:39pm
1
Continuing the discussion from RegEx for extracting value from string with trim :
Hello! I previously got help identifying a value by using regex. I needed to find a number by using a name (in this case Number 3). The structure of the example text was the following:
Number 6 Amount:44 dollars Number 3 Amount: 0,45 dollars Number 8 Amount:9,5 dollars Number 4 Amount:88 dollars
I now need to, in the case that this name (Number 3) occurs more than once: ADD its values together.
Number 6 Amount:44 dollars Number 3 Amount:0,45 dollars Number 8 Amount:9,5 dollars Number 4 Amount:88 dollars Number 3 Amount:1,42 Number 7 Amount:4,5
In this case I would want the robot to add 0,45 + 1,42 and have the result be 1,87 (values can differ).
agnesv
(Agnes)
February 19, 2020, 1:40pm
2
I have started by creating a counter which checks how many times this string (Number 3) occurs. I thought I could use a loop somehow by doing that (If Occurence < 1: loop every value and add them). But I am not sure how to do that or if there’s an easier way
bcorrea
(Bruno Correa)
February 19, 2020, 1:42pm
4
regex can return all occurrences of the same pattern and you loop through results and converting to decimal you can sum them together…
agnesv
(Agnes)
February 19, 2020, 1:46pm
5
How do I make it loop through the occurences?
If I use my code from previously:
System.Text.RegularExpressions.Regex.Match(EntirePage,“(?<= Number 3:)\s*[0-9.,]*(?= ha)”).Value
And there is multiple matches of this, it only returns the value of last time it occured.
If there is only one occurence, it returns the right and only value.
bcorrea
(Bruno Correa)
February 19, 2020, 1:48pm
6
you need Matches to get more than one possible occurrence
agnesv
(Agnes)
February 19, 2020, 1:51pm
8
“Value is not a member of System.Text.Reg…”
bcorrea
(Bruno Correa)
February 19, 2020, 1:54pm
9
remove the Value at the end… Matches will return a collection of Match object that you should use a For Each on…
Yoichi
(Yoichi)
February 19, 2020, 2:03pm
10
Hi,
Can you try the following expression?
dblVar = System.Text.RegularExpressions.Regex.Matches(yourString,"(?<=Number 3 Amount:\s*)[\d,]+").Cast(Of Match).Sum(function(x) double.Parse(x.value, System.Globalization.CultureInfo.CreateSpecificCulture("fr-Fr")))
Regards,
bcorrea
(Bruno Correa)
February 19, 2020, 2:05pm
11
what happens if not find any?
bcorrea
(Bruno Correa)
February 19, 2020, 2:32pm
13
the thing for this case is that he will not know if it is 0 because all matches were also 0 or because did not find anything…
agnesv
(Agnes)
February 19, 2020, 2:34pm
14
when i removed .value this error occured:
bcorrea
(Bruno Correa)
February 19, 2020, 2:35pm
15
i told you it was a collection… you need to change your variable type…
agnesv
(Agnes)
February 19, 2020, 2:40pm
16
Are you please able to give an example of how you mean?
I am getting error Can not convert type System.Text.RegularExpressions.MatchCollection to Collection of String
agnesv
(Agnes)
February 19, 2020, 2:50pm
18
Would this work however many times it occurs?
Yoichi
(Yoichi)
February 19, 2020, 11:24pm
20
Hi,
Yes. The following sample there are 4 “Number 3” in the string.
Sample20200220-1.zip (9.9 KB)
@bcorrea
the thing for this case is that he will not know if it is 0 because all matches were also 0 or because did not find anything…
I think it depends on business rule. In this case, the value always seems positive, so we can know there is no match string if the return value is 0.
Regards,
2 Likes
system
(system)
Closed
February 22, 2020, 11:24pm
21
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.