用Python提取增值税标识号

2024-10-03 00:23:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从文本中提取德国增值税编号(Umsatzsteuer IdentificationsNummer)

string = "I want to get this DE813992525 number."

我知道,这个问题的正确正则表达式是(?xi)^( (DE)?[0-9]{9}|)$。 根据我的demo,它工作得很好

我尝试的是:

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

Tags: to文本numbergetstringmatchdethis
1条回答
网友
1楼 · 发布于 2024-10-03 00:23:13

在字符串中搜索时,不要使用^$

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))

输出:

['DE813992525', 'DE813992526']

相关问题 更多 >