简体   繁体   English

正则表达式:查找字符串中所有出现的特定数字

[英]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. 在python中,我想找到此字符串中数字33的所有确切出现,并将其替换为另一个数字。

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 '. 此处所有出现的33(但不是3333和331)已被占位符'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. 但是,第一个和最后一个33不匹配。

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? 有人可以解释为什么NUMERIC_PATTERN2无法正常工作并提出解决方案吗? (I would prefer a solution which uses standard re module in python) (我更喜欢在python中使用标准re模块的解决方案)

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

Try this. 尝试这个。 See demo. 参见演示。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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