我試圖避免從字符串中提取 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 matched_wordsstring = "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']結(jié)果中的最后一個數(shù)字是德國 IBAN 號碼(第一部分),我不想提取它。我怎樣才能避免它?
1 回答

臨摹微笑
TA貢獻(xiàn)1982條經(jīng)驗 獲得超2個贊
您可以通過將空格設(shè)置為可選來縮短交替時間。如果您不需要最后一個數(shù)字,但確實需要以點結(jié)尾的數(shù)字,則可以斷言該模式后面沒有數(shù)字。
\b(?:DE[0-9 ]{12}|DE ?[0-9]{9})(?!\d)
對于第三個示例,您還可以使其更精確地匹配 3 乘以 3 個數(shù)字,前面有一個空格,也[0-9 ]{12}
可能匹配 12 個空格。
\b(?:DE(?: \d{3}){3}|DE ?[0-9]{9})(?!\d)
添加回答
舉報
0/150
提交
取消