0

I'm trying to match a regular expression that parses a response .....

error code|error text|submission reference
2|missing or invalid fields|0

it uses re.match(self.error_format)

I have tried error_format as:

(?P<status_code>[0-9]+)|(?P<status_message>.+)|(?P<gateway_message_id>[a-zA-Z0-9-]+)

but this matches the line error code|error text|submission reference not the second line as needed.

also tried:

(?P<status_code>[0-9]+)\|(?P<status_message>.+)\|(?P<gateway_message_id>[a-zA-Z0-9-]+)

but this does not match at all.

Update:

What I want to do is match only 2|missing or invalid fields|0 but the full text is error code|error text|submission reference 2|missing or invalid fields|0 so its like I need to skip the first part.

i.e.

msg = re.match('(?P<status_code>[0-9]+)\|(?P<status_message>.+)\|(?P<gateway_message_id>[a-zA-Z0-9-]+)', 'error code|error text|submission reference 2|missing or invalid fields|0')
2
  • I think the main thing that is hanging you up (and maybe others too) is that match will only find stuff right at the beginning of the text being searched. You need to use search if what you're looking for might not be at the beginning.
    – John Y
    Apr 3, 2013 at 21:38
  • Well, if the OP would split the lines and do re.match line by line it would actually work just as good as re.search. Otherwise, re.finditer should do the trick (I would imagine the real log file will have more than 1 matching line).
    – Martin B.
    Apr 4, 2013 at 8:46

3 Answers 3

2

Try not to match the separator. Like this:

 (?P<status_code>^[0-9][^|]*)\|(?P<status_message>[^|]+)\|(?P<gateway_message_id>.+)
8
  • This is a good point too, but I would only use that in the 2nd group - in the first group [0-9] wouldn't have matched it anyway and was probably more specific to the logging format of the OP.
    – Martin B.
    Apr 3, 2013 at 21:10
  • this seems to still only match the first line. I want it to skip the whole first line and only match on the second.
    – MarkO
    Apr 3, 2013 at 21:12
  • 1
    @MarkO I've updated the rule to match only lines starting with a number ( so the first one would be ignored ). Is that right for you?
    – Ander2
    Apr 3, 2013 at 21:17
  • 1
    this is what I'm using.... bitbucket.org/schinckel/django-sms-gateway/src/…
    – MarkO
    Apr 3, 2013 at 21:40
  • 1
    I would split the response (status_msg) in different lines and then apply the match rule.
    – Ander2
    Apr 3, 2013 at 21:51
2
msg = re.match('(?P<status_code>[0-9]+)\|(?P<status_message>[^|]+)\|(?P<gateway_message_id>[a-zA-Z0-9-]+)', '2|missing or invalid fields|0')

matches perfectly, and then you can access the individual parts via msg.group('status_code')

The version without the \ will also match, but it will only catch the "2" and won't fill all three groups in your 2nd line example.

If you want to run this on a text with multiple lines, you can do

matches = re.finditer('(?P<status_code>[0-9]+)\|(?P<status_message>[^|]+)\|(?P<gateway_message_id>[a-zA-Z0-9-]+)', s)
for m in matches:
    print m.group('status_code'), m.group('status_message'), m.group('gateway_message_id')

or then the other way around:

for line in youtext.split('\n'):
    m = re.match(msg = re.match('(?P<status_code>[0-9]+)\|(?P<status_message>[^|]+)\|(?P<gateway_message_id>[a-zA-Z0-9-]+)', line)
    if m:
        print m.group('status_code'), m.group('status_message'), m.group('gateway_message_id')

I think that covers all the options and none of them will match your first line that doesn't have a numeric error code in the first section.

3
  • thats seems to give me "error code|error text|submission reference" match which I want 2|missing or invalid fields|0
    – MarkO
    Apr 3, 2013 at 21:09
  • Not quite sure what you mean, but the above will not return a match for re.match('(?P<status_code>[0-9]+)\|(?P<status_message>.+)\|(?P<gateway_message_id>[a-zA-Z0-9-]+)', 'error code|error text|submission reference'), so it should be exactly what you wanted.
    – Martin B.
    Apr 3, 2013 at 21:14
  • 1
    PS. is your text really all in one line without any distinction between status messages spaces and spaces between "0|multi word msg|123 0|msg2|345" groups - if not, the re needs to be really smart about not getting lost in between the different log items, which is quite a bit more complicated to cover all possible cases.
    – Martin B.
    Apr 3, 2013 at 21:42
0
import re

tests = '''\
error code|error text|submission reference
2|missing or invalid fields|0'''.splitlines()

for test in tests:
    pat = r'''(?x)
        (?P<status_code>[^|]+)
        [|](?P<status_message>.+)
        [|](?P<gateway_message_id>[\w\d-]+)'''

    print(re.match(pat, test).groups())

yields

('error code', 'error text', 'submission')
('2', 'missing or invalid fields', '0')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.