Search code examples
pythonpython-3.xopencvcomputer-visionocr

Regular Expression for IFSC Code of Indian Bank


The IFSC code is consists of first 4 letters then '0' then 6 digits. The Regex is below [A-Z|a-z]{4}[0][\d]{6}$ But i am using OCR to extract text so i am cleaning the code

        ifsc_code = ifsc_code.replace(",","")
        ifsc_code = ifsc_code.replace("X","0")
        ifsc_code = ifsc_code.replace("o","0")
        ifsc_code = ifsc_code.replace("O","0")
        ifsc_code = ifsc_code.replace("(","")
        ifsc_code = ifsc_code.replace(")","")

so what it does it converts "ORBC0101346" to "0RBC0101346" and my regex fails. what i was thinking to do is to split this ifsc code into 2 parts firsts 4 characters and do the cleaning and then the second part consisting of numbers only and then join them together at the end after cleaning.


Solution

  • Continuing on my comment: You could use match groups to peel the different components apart, such as

    def parse_ifsc(ifsc_code):
        m = re.match('([A-Za-z0]{4})(0\d{6})$', ifsc_code)
        if m:
            return m.group(1).replace('0', 'O').upper() + m.group(2)
        else:
            return None