Using a regular expression to find specially formatted fields

In an application I am developing, someone thought it would be nice to include commas in the csv file values. So what I'm trying to do is select these values ​​and then separate the commas by those values. But the Regex I built won't fit to anything.

Regex pattern: .*,\"<money>(.+,.+)\",?.*

And the type of values ​​I am trying to match would be 2700, 2650 and 2600 in "2,700","2,650","2,600"

.

+1


source to share


2 answers


Thanks for Ryan's answer. I had to keep myself informed a little more. Though still, commas in CSV that do not separate values?

After asking about the office, I pointed to a free Regex builder app that I used to create the template I needed. Rad Software Regular Expression Designer App

Oh, and for the purposes of the answer, the pattern I came out with is this: \"(?<money>[0-9,.$]*)\"

Edit: Final Andwer



Wow. I completely forgot about this question. I dabbled in regex even more and came out with one that would match everything I needed. Regular expression:

\"([0-9.$]+(,[0-9.]+)+)\"

This regex managed to match any deciaml string inside the double quote I chose.

0


source


Commas are allowed in CSV and shouldn't cause problems if you are using a text classifier (usually double quote).

Here are the details: http://en.wikipedia.org/wiki/Comma-separated_values

In to regex:

This code works for your sample data (but only allows one comma, basically thousands of numbers separated up to 999 999):

string ResultString = null;
try {
    ResultString = Regex.Replace(myString, "([0-9]{1,3})(?:(,)?([0-9]{3})?)", "$1$3");
} catch (ArgumentException ex) {
    // Syntax error in the regular expression
}

      



The following is required:

Test 12,205 26,000 Test. And the sorting of the values ​​I am trying to match will be 2700, 2650 and 2600 in "2700", "2650", "2600"

and produces this:

Test 12205 26000 Test. And the sorting of the values ​​I'm trying to match would be 2700 2650 and 2600 in "2700", "2650", "2600"

+2


source







All Articles