Reputation: 887
I am trying to extract substring like ***.ini from string. For example, I have
000012: 378:210 File=test1.ini Cmd:send command1
000512: 3378:990 File=test2.ini Cmd:send command2 File=not.ini Cmd: include command
I need to extract the substring after the first "File=", and the substring after the first File=***.ini which is "Cmd: ..." till the end.
So the result I want is:
test1.ini
Cmd:send command1
and
test2.ini
Cmd:send command2 File=not.ini Cmd: include command
I tried:
re.match("(.*) File=(.*).ini(.*)Cmd:(.*)", line, re.M\re.I)
this works well with the first line, but for the second line, I get:
test2.ini Cmd:send command2 File=not.ini #which is wrong, wanted is:
test.ini
Cmd: include command
Anyone please help. Thanks. LJ
Upvotes: 0
Views: 116
Reputation: 34387
.* is too greedy, also there is no need to match from start of line. Try this
re.search("File=([^\.]+.ini).*?(Cmd:.*)", line).groups()
Upvotes: 1
Reputation: 785731
You can use this regex with re.findall
function:
\bFile=(.+?\.ini)\s+(Cmd:.*)
Code:
p = re.compile(ur'\bFile=(.+?\.ini)\s+(Cmd:.*)')
print re.findall(p, input_str)
Upvotes: 1