pythonregexrubular

Find all characters in string with comma using regular expression


I want to search all '2' s in the string below:

2, 10,11,1,1, 22,1,12,1,1, 1,2 , 2  ,2

I gave regular expression:

(^\s*(2)\s*)|(\s*,\s*(2)\s*(,|$))

But it will not search last but one 2.


I changed regular expression now as,

(^\s*(2)\s*)|(\s*,\s*(2)\s*)

But now it will take one of the 2s in 22.


How can I change the regular expression so that I find all the 2s (22 or 12 should not be considered)?


Solution

  • This will match all the standalone 2 in the input string:

    (?<!\d)2(?!\d)
    

    The regex above is equivalent to the following regex, as in it will always have the same number of matches, and the content of the capturing group 1 in this regex is the same as the content matched by the other regex:

    (?:^|\D)(2)(?:\D|$)
    

    Note that (both) regex above doesn't care about the format of the input. It just simply extract any standalone 2, even in:

    2 plain text, 2,43_2,lkjsf
    ^             ^    ^
    

    If you want to match columns with the value 2, and disregard 2 in 2 2 or abc 2:

    (?:^|,) *(2) *(?=,|$)
    

    It will match the only the 2 in this sample text:

    2 plain text, 2 ,2, 43_2, 2 2,2   ,234,2
                  ^  ^            ^        ^