我正在尝试使用python re正则表达式匹配文本然后任何数字,除非文本是某些单词。 e.g。
# import re
import re
# this match expression is intended to match any alphanumerical word followed by any number unless the first alphanumerical word ends with either germany or france.
match = r'[a-zA-Z0-9]{1,}[\s]{1,}(?<!france)(?<!germany)[0-9]{1,}'
re.findall( match, 'alphanumerical1234text 12312442')
>>>['alphanumerical1234text 12312442'] # this is correct
re.findall( match, 'alphanumerical1234text germany 12312442')
>>> ['germany 12312442'] # this shouldn't return anything
re.findall( match, 'alphanumerical1234textgermany 12312442')
>>>['alphanumerical1234textgermany 12312442'] # this shouldn't return anything
re.findall( match, 'alphanumerical1234text france 12312442')
>>>['france 12312442'] # this shouldn't return anything
re.findall( match, 'alphanumerical1234textfrance 12312442')
>>>['alphanumerical1234textfrance 12312442'] # this shouldn't return anything
任何想法如何构建这个正则表达式?
答案 0 :(得分:3)
你必须把看守放在空间之前。 …\s(?<!france)
相当于…\s
,因为任何以空格结尾的内容都不能以“e”结尾。
r'[a-zA-Z0-9]+(?<!france)(?<!germany)\s+[0-9]+'