Reputation: 2553
I want to get the value of name from fstr using RegEx in Python. I tried as below, but couldn't find the intended result.
Any help will be highly appreciaaed.
fstr = "MCode=1,FCode=1,Name=XYZ,Extra=whatever" #",Extra=whatever" this portion is optional
myobj = re.search( r'(.*?),Name(.*?),*(.*)', fstr, re.M|re.I)
print(myobj.group(2))
Upvotes: 1
Views: 63
Reputation: 239443
You may not believe, but the actual problem was ,*
, in your regular expression. It makes matching ,
optional. So, the second capturing group in your regex matches nothing (.*?
means match between zero to unlimited and match lazily) and it checks the next item ,*
, it also means match ,
zero or more times. So it matches zero times and the last capturing groups matches the rest of the string.
If you want to fix your RegEx, you can simply remove the *
after the comma, like this
myobj = re.search( r'(.*?),Name(.*?),(.*)', fstr, re.I)
print(myobj.group(2))
# =XYZ
Online RegEx demo (with the mistake)
Online RegEx demo (after fixing it)
But as the other answer shows, you don't have to create additional capture groups.
BTW, I like to use RegEx only when it is particularly needed. In this case, I would have solved it, without RegEx, like this
fstr = "MCode=1,FCode=1,Name=XYZ,Extra=whatever"
d = dict(item.split("=") for item in fstr.split(","))
# {'FCode': '1', 'Extra': 'whatever', 'Name': 'XYZ', 'MCode': '1'}
Now that I have all the information, I can access them like this
print d["Name"]
# XYZ
Simple, huh? :-)
Edit: If you want to use the same regex for one million records, we can slightly improve the performance by precompiling the RegEx, like this
import re
pattern = re.compile(r"Name=([^,]+)", re.I)
match = re.search(pattern, data)
if match:
match.group(1)
Upvotes: 2
Reputation: 99
try it
rule = re.compile(r"Name=(?P<Name>\w*),")
res = rule.search(fstr)
res.group("Name")
Upvotes: 0
Reputation: 32189
You can do it as follows:
import re
fstr = "MCode=1,FCode=1,Name=XYZ,Extra=whatever"
myobj = re.search( r'Name=([^,]+)', fstr, re.M|re.I)
>>> print myobj.group(1)
XYZ
Upvotes: 0