简体   繁体   中英

regex: find all occurences of a specific number in a string

In python, I would like to find all exact occurrences of the number 33 in this string and replace it with another number.

My input string is:

original = '33 he3333llo 331 42 I\'m a 32 string 30 33 a33a 33\n 33'

and my desired output is:

' NUMERO he3333llo 331 42 I\'m a NUMERO string 30 NUMERO a NUMERO a NUMERO \n NUMERO '

Here all occurrences of 33 (but not 3333, and 331) has been replaced with the placeholder ' NUMERO '.

I have tried by using:

NUMERIC_PATTERN = re.compile(r'([^\d]+?)%s([^\d]+?)'%(33),re.UNICODE|re.DOTALL )
original = '33 he3333llo 331 42 I\'m a 32 string 30 33 a33a 33\n 33'
print original
print re.findall(NUMERIC_PATTERN,original)
print re.sub(NUMERIC_PATTERN,r'\1 NUMERO \2', original)

Which gives "almost" the correct answer:

'33 he3333llo 331 42 I\'m a 32 string 30  NUMERO  a NUMERO a  NUMERO \n 33'

However, the first and the last 33 are not matched.

I thought this new expression should fix it but it doesn't (I include beginning and end of line as alternatives but it has the same result the first version):

NUMERIC_PATTERN2 = re.compile(r'([^\d^]+?)%s([^\d$]+?)'%(33),re.UNICODE|re.DOTALL )

Can anybody explain why NUMERIC_PATTERN2 does not work and suggest the solution? (I would prefer a solution which uses standard re module in python)

(?<!\d)33(?!\d)

Try this. See demo.

http://regex101.com/r/lS5tT3/18

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM