Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 避免从字符串中提取IBAN编号_Python_Regex_String_Extract_Iban - Fatal编程技术网

Python 避免从字符串中提取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

我试图避免从字符串中提取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数字,我不想提取它。我怎样才能避免它呢?

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

\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
。同时,我提取所有其他相关模式。我必须再检查一遍,但它看起来很有希望!