为了账号安全,请及时绑定邮箱和手机立即绑定

避免从字符串中提取 IBAN 号码

避免从字符串中提取 IBAN 号码

陪伴而非守候 2023-07-27 15:43:20
我试图避免从字符串中提取 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']结果中的最后一个数字是德国 IBAN 号码(第一部分),我不想提取它。我怎样才能避免它?
查看完整描述

1 回答

?
临摹微笑

TA贡献1982条经验 获得超2个赞

您可以通过将空格设置为可选来缩短交替时间。如果您不需要最后一个数字,但确实需要以点结尾的数字,则可以断言该模式后面没有数字。

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

正则表达式演示


查看完整回答
反对 回复 2023-07-27
  • 1 回答
  • 0 关注
  • 101 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信