避免从字符串中提取 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 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 号码(第一部分),我不想提取它。我怎样才能避免它?


陪伴而非守候
浏览 102回答 1
1回答

临摹微笑

您可以通过将空格设置为可选来缩短交替时间。如果您不需要最后一个数字,但确实需要以点结尾的数字,则可以断言该模式后面没有数字。\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)正则表达式演示
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python