C# regex for ignoring commas in numbers

Hi Folks,

I’m working in c# and have strings like

“haha, whoa, 234, $2,999, 3,434,234, conclude”

where I need to split it on the commas. However, I want to ignore commas in numbers, including currency and percents, as well as plain numbers. What I want as a result is:


Is this possible?


Hi lianaent, welcome to the forums

How to distinguish delimiters when they are also valid in strings. That is a tough one!

I have littile to no experience with c#, but my first questions would be:
where are the coming from? database, CSV file, etc
are the fields consistant? i.e. always in the same sequence
are there “escapes” in the results?

I think it comes down to the format of the string. If you have any control over that, then use a different separator. Technically for lists you should use a semi-colon. However, if you have no control over that, I think you’ll find it next to impossible.

Using the string you posted above (which includes spaces), its very simple to match each value:

([A-Za-z]+|[0-9\\,\\$\\%]+[^\\, ])

Mittineague - excellent questions. I had to go back to my work computer and realized that some strings were coming from the database, and while I have no control over what’s in the database I can certainly control what comes out. Seems basic at first, but trust me, the code is rather complex in that I’m taking a SOAP string from an external client (no control there), going against out database to pull records, converting the results to a 270 file structure, etc. So the database part is resolved, but the strings coming in from external clients I have no control over and could still need to be parsed.

As to your other questions, there are always 31 fields coming in and yes, the order is predetermined, and there are no escapes.

mickyginger - you’re right about the spaces (input.split(’ ')), but that’s just the way I typed it. No, there aren’t spaces. But your pattern works beautifully (with minor tweaking for empty values).

So, I declare this resolved - THANKS!!!