Adding all values with same name together by using regex

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).

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

image

regex can return all occurrences of the same pattern and you loop through results and converting to decimal you can sum them together…

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.

you need Matches to get more than one possible occurrence

“Value is not a member of System.Text.Reg…”

remove the Value at the end… Matches will return a collection of Match object that you should use a For Each on…

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,

what happens if not find any?

what happens if not find any?

It returns 0.

1 Like

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… :wink:

when i removed .value this error occured:

image

i told you it was a collection… you need to change your variable type…

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

Would this work however many times it occurs?

(Not only twice)

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

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.