我正在尝试排除由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)
答案 0 :(得分:4)
You may use this regex:
(?<![N\d.])(\d+)(?:\.\d+)?
(?<![N\d.])
is negative look-behind assertion that fails the match if previous character is N
or digit or dot.