I want to extract the string with the following pattern.
MsgTrace(65/26)noop:user=xxx=INBOX:cmd=534
regex should extract noop
but when i try the follwong pattern , it extract the string "user" as well.
ImapFetchComplete(56/39)user=xxxxxxxxxx
need to output the word only contains the following pattern.
)somestring: (it should extract the word somestring)
)somestring=xxxx (this shouldn't be extracted)
#!/usr/bin/python
import os
from subprocess import *
import os
import re
dir="/tmp/logs/"
os.chdir(dir)
for filename in os.listdir(dir):
with open(filename) as fp:
for line in fp:
try:
print(re.search(r'\)([a-z]*?):',line).group(1))
except:
pass
somestringin)somestring=xxxxbecause there is no:.r'\)([^:]*?):user='so that you don't end up picking up the rest of the line. And I think you probably want to be usingre.match()instead ofre.search()if you're expecting the match to start at the beginning of the string. You'll need to adjust the regex a little more if you usere.match()though.