0

I have a log file in while I want to search for a pattern involving multiple lines using regular expression. From what I know, I am able to read and match only one line at once. How do I load a whole file in memory and do pattern matching?

I have a log file in which I am looking for assert message like this:

*** ASSERT: 0xb83c6056

    *** type of assert: <xyz>

    *** file: .

    *** firmware revision: <xyz>

    *** firmware build timestamp: <xyz>

    *** seconds since 1970: <xyz>

    *** at address: <xyz>

    *** fault address: xyz>

    *** Error sub-type: <xyz>

I am using the following regular expression.

re.compile(r'(\*){3}(\s){1}(ASSERT: )(.+)(\n){2}(\t)(\*){3}(\s){1}(type of assert: )(.+)(\n){2}(\t)(\*){3}(\s){1}(file: )(.+)(\n){2}(\t)(\*){3}(\s){1}(firmware revision: )(.+)(\n){2}(\t)(\*){3}(\s){1}(firmware build timestamp: )(.+)(\n){2}(\t)(\*){3}(\s){1}(seconds since 1970: )(.+)(\n){2}(\t)(\*){3}(\s){1}(at address: )(.+)(\n){2}(\t)(\*){3}(\s){1}(fault address: )(.+)(\n){2}(\t)(\*){3}(\s){1}(Error sub-type: )(.+)')

Please tell me how can I improve my regular expression in order to load the whole file at once and do the regex matching. Right now with my knowledge I am able to read line by line only.

CinCout
  • 9,486
  • 12
  • 49
  • 67
  • 1
    Please [edit] your post and show us what Python code you already have. Without seeing your Python code, we can't really help you improve it. – Corion Dec 23 '18 at 09:22
  • Please go over [ask] and [help/on-topic] again and if you have questions provide your code as [mcve]. We can not see _how_ you regex your file. Normally you can simply use file.read() to get all from the file and re.MULTILINE + re.DOTALL to match multiline parts ... – Patrick Artner Dec 23 '18 at 09:32
  • You can use `lines=file.readlines()`, to read the entire file and then use `'\n'.join(lines)`, to get the entire file as one string. And a not on your regex, you are creating way to many subgroups, try removing subgroups, that you don't want to capture – Pranav Chaudhary Dec 23 '18 at 18:48

0 Answers0