简体   繁体   中英

Get particular information from a string

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))

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)

正则表达式可视化

Debuggex Demo

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)

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

try it

rule = re.compile(r"Name=(?P<Name>\w*),")
res = rule.search(fstr)
res.group("Name")

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM