Extract IBAN from text with Python
ISO landcode | Verification# | Bank# | Account# | |
---|---|---|---|---|
Germany | 2a | 2n | 8n | 10n |
Austria | 2a | 2n | 5n | 11n |
Note: a - alphabets (letters only), n - numbers (numbers only)
So the main difference is really the length in digits. That means you could try:
\b(?:DE(?:\s*\d){20}|AT(?:\s*\d){18})\b(?!\s*\d)
See the online demo.
\b
- Word-boundary.(?:
- Open 1st non-capturing group.DE
- Match uppercase "DE" literally.(?:
- Open 2nd non-capturing group.\s*\d
- Zero or more spaces upto a single digit.){20}
- Close 2nd non-capturing group and match it 20 times.
|
- Or:AT
- Match uppercase "AT" literally.(?:
- Open 3rd non-capturing group.\s*\d
- Zero or more spaces upto a single digit.){18}
- Close 2nd non-capturing group and match it 20 times.
)
- Close 1st non-capturing group.
\b
- Word-boundary.(?!\s*\d)
- Negative lookahead to prevent any trailing digits.
It does show that your Austrian IBAN numbers are invalid. If you wish to extract up to the point where they would still be valid, I guess you can remove \b(?!\s*\d)