正则表达式:从字符串中搜索一个数字并排除数字(可以是整数/浮点数),这是由“N'

时间:2018-06-19 11:00:15

标签: python regex python-3.x

我正在尝试排除由N引导的数字。为此,我使用了反向跟踪,下面是我使用的正则表达式从表达式中排除N10.5。

expression = "N10.5*33+31"
variable_refs = re.compile(r'\b(?<!N)([0-9])+(\.)?(\d+)?')
exp_template = re.sub(variable_refs, r'{key_\1}', expression)
print(exp_template)

输出将是: (key_33,key_31)

1 个答案:

答案 0 :(得分:4)

You may use this regex:

(?<![N\d.])(\d+)(?:\.\d+)?

RegEx Demo

(?<![N\d.]) is negative look-behind assertion that fails the match if previous character is N or digit or dot.