forked from deepdalsania/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtractPID.py
More file actions
20 lines (17 loc) · 841 Bytes
/
ExtractPID.py
File metadata and controls
20 lines (17 loc) · 841 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import re
log = "May 21 08:07:10 mycomputer bad_process[12345]: ERROR Performing package upgrade"
pattern = r'\[(\d+)\]'
print("PID : ", re.search(pattern, log)[1])
# checking regex on other strings
print("Different String (1) : ", re.search(pattern, "A completely different string that also has numbers [34567]")[1])
# it gives TypeError: 'NoneType' object is not subscriptable
#print("Different String (2) : ", re.search(pattern, "99 elements in a [cage]")[1])
print("Extract PID Method in Action")
def extract_pid(log):
pattern = r'\[(\d+)\]'
result = re.search(pattern,log)
if result is None:
return ""
return result[1]
print("Extract PID (1) : ",extract_pid("May 21 08:07:10 mycomputer bad_process[12345]: ERROR Performing package upgrade"))
print("Extract PID (2) : ",extract_pid("99 elements in a [cage]"))