I am trying to extract this text \"NL dd ABNA ffffdffffdffffd\" from string:
IBAN NL 91ABNA0417463300
IBAN NL91ABNA0417164300
Iban: NL 69 ABNA 402032566
The problem in your regex101 demo is, there is an extra character in your regex after $
so remove that and change 0
to [0 ]
and this fixes all and starts matching your third line too. The correct regex becomes,
NL\s?\d{2}\s?[A-Z]{4}[0 ]\s?\d{9}$
Check your updated demo
It's not what you want, but works.
IBAN has a strict format, so it's better to normalize it, and next just cut part, because everything will match regexp, as an example:
CODE
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# I'm not sure, that alphabet is correct, A-Z, 0-9
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
def normalize(string):
stage1 = "".join(IBAN.split()).upper()
stage2 = ''
for l in stage1:
if l in alphabet:
stage2 = stage2 + l
return stage2.split('IBAN')[1]
if __name__ == '__main__':
IBAN_LIST = ['IBAN NL 91ABNA0417463300', 'IBAN NL91ABNA0417164300', 'Iban: NL 69 ABNA 402032566']
for IBAN in IBAN_LIST:
IBAN_normalized = normalize(IBAN)
print(IBAN_normalized[2:4], IBAN_normalized[8:])
OUTPUT
91 0417463300
91 0417164300
69 402032566
It's not a regexp, but should work faster, but if you know how to normalize better, please, help with it.
You can see source code here.
You can just remove all spaces and uppercase the rest, Like this:
iban = NL 91ABNA0417463300
iban.replace(" ", "")
iban.upper()
And then your regex would be:
NL\d{2}ABNA(\d{10}|\d{9})
It works in https://regex101.com/r/zGDXa2/1
You can use the following regex:
(?i)(?:(?<=IBAN(?:[:\s]\s|\s[:\s]))NL\s?\d{2}\s?[A-Z]{4}[0 ]\s?\d{9,10})|(?:(?<=IBAN[:\s])NL\s?\d{2}\s?[A-Z]{4}[0 ]\s?\d{9,10})
demo:
https://regex101.com/r/zGDXa2/11
If you work in python you can remove the (?:i)
and replace it by a flag re.I
or re.IGNORECASE
Tested on:
Uw BTW nummer NL80
IBAN NL 11abna0317164300asdfasf234
iBAN NL21ABNA0417134300 22
Iban: NL 29 ABNA 401422366f sdf
IBAN :NL 39 ABNA 0822416395s
IBAN:NL 39 ABNA 0822416395s
Extracts:
NL 11abna0317164300
NL21ABNA0417134300
NL 29 ABNA 401422366
NL 39 ABNA 0822416395
NL 39 ABNA 0822416395