Regex for IBAN mask

后端 未结 4 1098
心在旅途
心在旅途 2021-01-22 05:18

I am trying to extract this text \"NL dd ABNA ffffdffffdffffd\" from string:

IBAN NL 91ABNA0417463300
IBAN NL91ABNA0417164300
Iban: NL 69 ABNA 402032566
4条回答
  •  生来不讨喜
    2021-01-22 06:09

    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.

提交回复
热议问题