Matches not generating correct result

Hello, I’m having trouble with Matches.

the text for testing:
Standard Supply Service Administrative Charge
at $0.25 per 30 Days 0.25
Wholesale Market Service Charge We’ve launched a new map to help make it easy to
114,794.214 kWh at $0.0039 per kWh 447.70 locate and report streetlight issues online. To report a
Your Total Electricity Charges 14,172.07 streetlight out or check the status of an existing
H.S.T. (H.S.T. Registration 896718327RT0001) 1,842.37 report, visit

Regex I have:
(?<=Wholesale Market Service Charge).\r?\n.?[\d.,]+ kWh at $[\d.,]+ per kWh ([\d.,]+)

the regex works fine as attached (first attachment) and it captures the number I need. However when I run the bot and uses a for each and msg box to show the matchResult.groups(0).value I got the string as shown in the second attachment. I set the property of matches to multiline already. Does anyone know why and how to fix this?

thanks!


Capture1

Hi,

In my environment, your pattern returns the following as you expected.

If you want to remove “We’ve…”, the following might work.

System.Text.RegularExpressions.Regex.Matches(yourString,"(?<=Wholesale Market Service Charge.*)\r?\n.*?[\d.,]+ kWh at \$[\d.,]+ per kWh ([\d.,]+)",System.Text.RegularExpressions.RegexOptions.Multiline)

Regards,

1 Like

Hi @lynnsong986, as @Yoichi was correctly saying, your pattern is working, but it’s designed to best work with “Match” instead of “Matches”

If you see on your image above, The text “We’ve…” is also selected, therefore being included in your “Matches” result.

If what you want is the number “447.70”, you might want to consider changing to “Match”, and then you can make use of your Groups in the expression

([\d.,]+)

image

System.Text.RegularExpressions.Regex.Match(yourString,"(?<=Wholesale Market Service Charge.*)\r?\n.*?[\d.,]+ kWh at \$[\d.,]+ per kWh ([\d.,]+)").Groups(1).ToString

Cheers!

Thanks both, Yoichi’s solution works! very much appreciated!!

2 Likes

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