SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Nov 2013
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

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

    Is this possible?

    Thanks!

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,228
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)
    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?

  3. #3
    SitePoint Addict
    Join Date
    Oct 2009
    Location
    London, UK
    Posts
    382
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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:

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

  4. #4
    SitePoint Member
    Join Date
    Nov 2013
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!!!!!


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •