用Python提取增值税标识号
我试图从文本中提取德国增值税编号(Umsatzsteuer IdentificationsNummer)用Python提取增值税标识号,python,regex,Python,Regex,我试图从文本中提取德国增值税编号(Umsatzsteuer IdentificationsNummer) string = "I want to get this DE813992525 number." 我知道,这个问题的正确正则表达式是(?xi)^((DE)?[0-9]{9}|)$。 根据我的经验,它工作得很好 我尝试的是: string = "I want to get this DE813992525 number. match = re.compile(
string = "I want to get this DE813992525 number."
我知道,这个问题的正确正则表达式是(?xi)^((DE)?[0-9]{9}|)$
。
根据我的经验,它工作得很好
我尝试的是:
string = "I want to get this DE813992525 number.
match = re.compile(r'(?xi)^( (DE)?[0-9]{9}|)$')
print(match.findall(string))
>>>>>> []
我想得到的是:
print(match.findall(string))
>>>>> DE813992525
在字符串中搜索时,不要使用
^
和$
:
import re
string = """I want to get this DE813992525 number.
I want to get this DE813992526 number.
"""
match = re.compile(r'DE[0-9]{9}')
print(match.findall(string))
输出:
为什么不仅仅是
^DE[0-9]{9}$
请参见“否”,它是-例如,$
锚点表示字符串结束,而您的测试字符串VAT编号不在末尾。非常感谢!如果数字是分开的:DE 813992525
和DE 813992526
或者可以使用regexp中的或
操作符:DE[0-9]{12}DE[0-9]{9}
['DE813992525', 'DE813992526']