Search code examples
pattern-matchingmt940

Regular Expression how to print one of two in matching pattern


I have a problem with Regular Expressions. I'm writing a small program that matches a line from mt940 bank statement file. I have a simple string like this one:

:60F:C120613PLN245265,82

And a regular expression:

([C|D]{1})|([0-9]{6})|([A-Z]{3})|([0-9]+(\,[0-9]{2}))

that returns me

C
120613
PLN
143783 
4,82

But the last two rows are separated, and i would recaived not separated last two rows, like this:

C
120613
PLN
1437834,82

I think, that the last rows has a 6 numbers before ",", that matches to a second pattern in my whole pattern.

What I can do to recaive the last two words not separated?


Solution

  • This pattern works for me:

    pattern "([C|D]{1})([0-9]{6})([A-Z]{3})([0-9]+,[0-9]{2})"
    

    I've tested it in Python:

    re.findall(pattern, ":60F:C120613PLN245265,82") // [('C', '120613', 'PLN', '245265,82')]