Deal all
I faced some not trivial problem for me to parse log.
I need to go through a file and check if the line matches the patter : if YES then get ClientID specified in this line.
The line looks like :
17.02.09 10:42:31.242 TRACE [1245] GDS: someText(SomeText).ClientID: '' -> '99071901'
So I need to get 99071901.
I tried to construct regexp search pattern, but it is not complete..stuck at 'TRACE':
regex = '(^[(\d\.)]+) ([(\d\:)]+) ([\bTRACE\b]+) ([(\d)]+) ([\bGDS\b:)]+) ([\ClientID\b])'
Script code is :
log=open('t.log','r')
for i in log:
key=re.search(regex,i)
print(key.group()) #print string matching
for g in key:
client_id=re.seach(????,g) # find ClientIt
log.close()
Appreciate if you give me a hint how to solve this challenge.
Thank you.
r"'(\d+)'$"regex and grab.group(1). Else, to find other "submatches", "spell out" the pattern, like^([\d.]+)\s+([\d.:]+)\s+(TRACE)\s+\[(\d+)] GDS:.*?ClientID:\s*''\s*->\s*'(\d+)'$and access the groups you need using appropriate indices.m = re.search(pat, s),if m: print(m.group(n))wherenis the group ID. See the Python demo.