Skip to content

Instantly share code, notes, and snippets.

@Codehunter-py
Created March 6, 2022 16:36
Show Gist options
  • Save Codehunter-py/e57cf2a6f55995f1bf76e456a794decc to your computer and use it in GitHub Desktop.
Save Codehunter-py/e57cf2a6f55995f1bf76e456a794decc to your computer and use it in GitHub Desktop.
Regular expression used in the extract_pid function, to return the uppercase message in parenthesis, after the process id.
import re
def extract_pid(log_line):
regex = r"\[(\d+)\]\: (\w+)"
result = re.search(regex, log_line)
if result is None:
return None
return "{} ({})".format(result[1], result[2])
print(extract_pid("July 31 07:51:48 mycomputer bad_process[12345]: ERROR Performing package upgrade")) # 12345 (ERROR)
print(extract_pid("99 elephants in a [cage]")) # None
print(extract_pid("A string that also has numbers [34567] but no uppercase message")) # None
print(extract_pid("July 31 08:08:08 mycomputer new_process[67890]: RUNNING Performing backup")) # 67890 (RUNNING)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment