Python 避免从字符串中提取IBAN编号
我试图避免从字符串中提取IBAN编号 例如:Python 避免从字符串中提取IBAN编号,python,regex,string,extract,iban,Python,Regex,String,Extract,Iban,我试图避免从字符串中提取IBAN编号 例如: def get_umsatzsteuer_identifikationsnummer(string): # Demo --> https://regex101.com/r/VHaS7Y/1 reg = r'DE[0-9 ]{12}|DE[0-9]{9}|DE [0-9]{9}' match = re.compile(reg) matched_words = match.findall(string) return m
def get_umsatzsteuer_identifikationsnummer(string):
# Demo --> https://regex101.com/r/VHaS7Y/1
reg = r'DE[0-9 ]{12}|DE[0-9]{9}|DE [0-9]{9}'
match = re.compile(reg)
matched_words = match.findall(string)
return matched_words
string = "I want to get this DE813992525 and this DE813992526 number and this
number DE 813 992 526 and this number DE 813992526. I do not want the bank
account number: IBAN DE06300501100011054517."
get_umsatzsteuer_identifikationsnummer(string)
>>>>> ['DE813992525',
'DE813992526',
'DE 813 992 526',
'DE 813992526',
'DE063005011000']
结果中的最后一个数字是(第一部分)德国IBAN数字,我不想提取它。我怎样才能避免它呢?您可以通过将空格设置为可选来缩短替换时间。如果您不想要最后一个数字,但想要以点结尾的数字,则可以断言模式后面没有数字
\b(?:DE[0-9 ]{12}|DE ?[0-9]{9})(?!\d)
对于第三个示例,您还可以使其更精确地匹配3乘以3个数字,前面有一个空格,因为[0-9]{12}
也可能匹配12个空格
\b(?:DE(?: \d{3}){3}|DE ?[0-9]{9})(?!\d)
你可以缩短模式,使空格成为可选的
\b(?:DE[0-9]{12}DE?[0-9]{9}(?!\d)
并在可能更全面一点的r'\b(?:DE(?:\d{3}(?:\d{3}{2}{d{0-9]{9})(?!\d}\d}\d{3}d}d}之后不断言一个数字。哦,我认为你的第二个例子很好。它不提取银行账号,如DE06300501100011054517
和DE89 3704 0044 0532 0130 00
和DE12 1234 5678 0000 0123 45
。同时,我提取所有其他相关模式。我必须再检查一遍,但它看起来很有希望!