0

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)?

Cœur
  • 37,241
  • 25
  • 195
  • 267
SHRI
  • 2,406
  • 6
  • 32
  • 48

3 Answers3

1

Use a negative lookbehind and negative lookahead.

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

DEMO

>>> import re
>>> s = "2, 10,11,1,1, 22,1,12,1,1, 1,2 , 2  ,2"
>>> re.findall(r'(?<!\d)2(?!\d)', s)
['2', '2', '2', '2']

Explanation:

  • (?<!\d)2 Lookbehind asserts that the 2 would be preceded by any but not a digit.
  • 2(?!\d) Asserts that the 2 must be followed by any but not of a digit.
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
1

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
              ^  ^            ^        ^
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
  • If I "2,2 2,2" How can ignore 2 with space in between in the same regular expression – SHRI Oct 15 '14 at 06:59
  • @SHRI: Your new requirement will invalidate all the answers here, so it belongs to a new question. It is your fault for not specifying all the details. – nhahtdh Oct 15 '14 at 07:02
  • I did not specify because, it is a 'nice to have' requirement for me. That's why I still accepted your answer. Please provide if it not so complex – SHRI Oct 15 '14 at 07:03
0

Hopes this is what you are looking for

 [^\d]2[^\d]

http://regex101.com/r/nD6nZ9/1

the python code must be something like

p = re.compile(ur'[^\d]2[^\d]', re.MULTILINE)
nu11p01n73R
  • 26,397
  • 3
  • 39
  • 52