How can I get the sum of all regex matches in a string?

I have a regex, which gives me the numbers before “invoice” in a text, e.g. 3x invoice -> 3:
System.Text.RegularExpressions.Regex.Match(stringVar,"(\d+)(?=x invoice)").value

Problem:
Now the thing is that it can occur multiple times, e.g. 2x invoice, …, 3x invoice
I would like to get the sum of all numbers that match in Regex, e.g. 5 in the example before
Is this possible?
Thank you

Hi,

As far as I understood your input line would be “2x invoice,20x invoice, 3x invoice” and so on.

I think you can use Matches activity with you reg exp expression. This will return an array of all successful matches. Than you can use “For Each” to go through array to get a sum. Example workflow attached Main.xaml (10.2 KB)

2 Likes

Perfect, thanks a lot for the xaml file :slight_smile:

@Jizh
You can also try like this

strSum=(From p In (System.Text.RegularExpressions.Regex.Matches(strInput,"[0-9]+?(?=x invoice)"))
             Select Convert.ToString(p)).ToList.Sum(Function(x) Convert.ToInt32(x)).ToString

Regards,
Mahesh

1 Like

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