I am trying to sort through a some files using regex expression.
I have a file which contains the two following lines
NET "MBC_ADR_I1<1>" LOC = "R2";
NET "GP_O<7>" LOC = "R20";
I am using the following expression to get one of the lines only
f2MatchLoc = re.search('(LOC)[ ]+=[ ]+["]?({})'.format(f1LocValue), f2Line, re.IGNORECASE)
where f1LocValue = R2
. However I'm getting a match on both lines.
I've tried to enter the same expression here regex101.com
which shows that my argument should be correctly formatted
f2MatchLoc = re.search(r'(LOC)[ ]+=[ ]+["]?({}\b)'.format(f1LocValue), f2Line, re.IGNORECASE)
^^
You need to use \\b
after R2
so that there are no partial matches. See demo . Also use r
or raw
mode.
Because you have no conditions how the string should end.
'(LOC)[ ]+=[ ]+["]?({})'
^??
So it matches anything that starts with LOC = "R2
. Following are all valid search results
LOC = "R2
LOC = "R2asd
LOC = "R2121
LOC = "R2 "
Simply, you can use double quotes or semicolon to identify end of search string. Also you can replace \\s
for white-space capturing and you can remove []
around single element lists
r'(LOC)\s+=\s+"?({})"?;'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.