Variable string manipulation

Hello,

I am extracting data into a data table. One of the fields contains variable pieces of information
.
It could be:
CO-45: $40.88 , CO-253: $1.59 PR-2: $19.82
or it could be: CO-45: $40.88 , CO-253: $1.59
or:
CO-45: $139.41

There can be a different number of CO codes and of PR codes, but they are all listed in one field.
Part of the goal is to sum all the CO codes values and compare the sum with another value I have.

Currently I am using REGEX expressions to get up to 2 CO codes and one PR and assign them to variables. If the value is null then I assign 0.

Once I run into a different scenario, then of course Regex is not going to work as expected.

In your experience, is there a better way to handle the string so that it could correctly grab all the CO codes and PR codes no matter how many there are and (possibly using a loop and counter) assign them to variables like CO1code, CO2code, etc…

Or better yet, a way to grab all the values associated to however many CO codes, add them and save the sum to a single COSum variable.

Thank you all!

Use RegEx.Matches (not .Match) so you’ll get an array of all matching elements (specific datatype is actually MatchCollection but you treat it like an array). Then define it so it’ll find anything that’s two letters then a dash etc. Then you’ll have all those values in an array and can process them however you want.

([A-Z]*-[0-9]*: \$[^\s]+)

image

Hello Paul,

Thank you so much for the advice.
Using your information, I was able to accomplish the goal I had in mind.

Much appreciated.

1 Like

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