0

I am very much new to regex and I need to create one to parse logs into a shorter subset of that log message.

For instance, I have the log:

System trap received SystemString=Mon Jan 07 15:36:07 2017. System 1464333684: Type: Timing Quality le-5 System, Class: Timing, Level: None, Action: Clear System, Flags: 0x15

And I would like to parse and retrieve from it:

Timing Quality le-5 System, Class: Timing

At first this looked really trivial to me, but after watching many tutorials on regex... I am confused on how to approach such a task. Where do I begin?

Thanks in advance.

noob
  • 3,414
  • 4
  • 15
  • 28
  • Which programming language? – Mohammad Yusuf Jan 16 '17 at 09:55
  • @MYGz I don't know of those details... I've just been given the logs. The parsing will be done by networking equipment and I have been asked to come up with regexs' – noob Jan 16 '17 at 09:58
  • @noob that is a very vague question. – Mohammad Yusuf Jan 16 '17 at 10:00
  • 2
    SO is for getting help with specific coding questions. You're supposed to post your own coding attempt (preferably with some sample input, expected output, and actual output) and explain why you're stuck. If you can't even tell us what language you're using the question is _way_ too broad. – PM 2Ring Jan 16 '17 at 10:16

1 Answers1

0

If your string is in s I would do:

g = re.search('Timing Q.*?Class.*?(?:,)', s)
print(g.group(0))

I first search Timing followed by Q, and continue with all characters with non-greedy manner (.*?) until I meet Class and until the following comma. I do not capture the comma itself, by designating the comma within a non-capturing parenthesis - (?:,)

Israel Unterman
  • 11,748
  • 2
  • 22
  • 31
  • Hi, thanks for the repsonse... this works for the particular log I've given. But the type may be different from Timing Quality.. so I've changed it to `(?:Type).*?Class.*?(?:,)` . However when I test this in [regex101](https://regex101.com) the text Type and the comma at the end are still shown and are not non-captured? – noob Jan 16 '17 at 10:29
  • You are right, I tested it on regex 101, for some reason it gives the full match including the non-capturing parenthesis. But it works correctly in Python – Israel Unterman Jan 16 '17 at 12:11
  • This helped. Many thanks! – noob Jan 17 '17 at 11:28