Using Regex.Matches to extract a number (w/ commas) from a text file

I have a large text file I need to get a number from. I use the ‘Text to Left/Right’ activity then …Regex.Matches… to get the number (see below). When the number does not have commas, it works great. However, when commas are present, it pulls the number(s) to the left of the first comma. For example, if the number is 867,281 it pulls 867. I need it to pull 867281.

System.Text.RegularExpressions.Regex.Matches(Saved.Values(Of System.String)(“TextRight”),“\d+”)(0).ToString

Any help is greatly appreciated!

Hi @excelknut

can you give a try with the following

System.Text.RegularExpressions.Regex.Match(Saved.Values(Of System.String)("TextRight"), "\d{1,3}(,\d{3})*").Value.Replace(",", "")

Regards

1 Like

@excelknut

(\d+[,]\d+) then replace(“,”,“”)

try this pattern pls

Hi,

FYI, another approach:

In this case, we can replace comma with blank in advance, then use \d+

System.Text.RegularExpressions.Regex.Match(Saved.Values(Of System.String)("TextRight").Replace(",",""),"\d+").Value

OR

Extract it with thousand separator and/or decimal separator, then parse it into double value or replace it with blank

strVal = System.Text.RegularExpressions.Regex.Match(Saved.Values(Of System.String)("TextRight"),"\d[\d.,]*").Value

Then

doubleVar = Double.Parse(strVal,System.Globalization.NumberStyles.Any)

Regards,

WOW! Thank you so much! I spent hours/days on google and YouTube looking for an answer to no avail and this forum provided the solution in minutes. I REALLY appreciate it!

1 Like

Thanks to all for taking the time to assist.

BTW, I used the expression from fernando_zuluaga.

Rageena_M, I couldn’t understand how to get that to work.

Yoichi, I’ll try the expressions you provided.

3 Likes

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