Search code examples
regexpcre

How to parse comma separated string without split using regular expression


The main problem that I do not have any split, match or other functions because it's not a program, I have only regexp support. The string is:

145,453,234

I extracted last value using this ([0-9]{3})(?!,) and this works perfect.

How to deal with first two values (I need two regexps)?


Solution

  • To have a regex specifically for each section you should care about boundaries. If all chars between commas are numbers then use these regexes to get each part separately:

    • First part: ^\d+
    • Second part: (?<=,)\d+(?=,)
    • Last part: \d+$

    If it could be more than numbers use [^,] in place of \d.