The regular expression MESSAGE_REGEX does not allow whitespace (or newlines) between each header. For example, if the test MESSAGE_1 is defined as:
MESSAGE1 = """{1:F01ASDFJK20AXXX0987654321}
{2:I103ASDFJK22XXXXN}
{4: :20:20180101-ABCDEF :23B:GHIJ :32A:180117CAD5432,1 :33B:EUR9999,0 :50K:/123456-75901 SOMEWHERE New York 999999 GR :53B:/20100213012345 :57C://SC200123 :59:/201001020 First Name Last Name a12345bc6d789ef01a23 Nowhere NL :70:test reference test reason payment group: 1234567-ABCDEF :71A:SHA :77B:Test this
-}"""
It does not parse:
>>> import mt103
>>> message = mt103.MT103(MESSAGE1)
>>> message.text
>>>
Redefining the regex to accept whitespace characters between headers:
MESSAGE_REGEX = re.compile(
r"^"
r"({1:(?P<basic_header>[^}]+)})?\s*"
r"({2:(?P<application_header>(I|O)[^}]+)})?\s*"
r"({3:"
r"(?P<user_header>"
r"({113:[A-Z]{4}})?"
r"({108:[A-Z 0-9]{0,16}})?"
r"({111:[0-9]{3}})?"
r"({121:[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-4[a-zA-Z0-9]{3}-[89ab][a-zA-Z0-9]{3}-[a-zA-Z0-9]{12}})?\s*" # NOQA: E501
r")"
r"})?"
r"({4:\s*(?P<text>.+?)\s*-})?\s*"
r"({5:(?P<trailer>.+)})?"
r"$",
re.DOTALL
)
solves the issue
>>> import mt103
>>> message = mt103.MT103(MESSAGE1)
>>> message.text
:20:20180101-ABCDEF :23B:GHIJ :32A:180117CAD5432,1 :33B:EUR9999,0 :50K:/123456-75901 SOMEWHERE New York 999999 GR :53B:/20100213012345 :57C://SC200123 :59:/2010
01020 First Name Last Name a12345bc6d789ef01a23 Nowhere NL :70:test reference test reason payment group: 1234567-ABCDEF :71A:SHA :77B:Test this
The regular expression
MESSAGE_REGEXdoes not allow whitespace (or newlines) between each header. For example, if the testMESSAGE_1is defined as:It does not parse:
Redefining the regex to accept whitespace characters between headers:
solves the issue