By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,703 Members | 1,116 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,703 IT Pros & Developers. It's quick & easy.

re.search making no match == 0

P: n/a
Hey all,

I have a search:

VAR = re.search("PROVEN.*?\n[\sA-Za-z\(\)\/\-]+\d(.*?)\n.*?" , pcpT9, re.S ).group(1) #.split()[1]

that does not match (sometimes it will and sometimes it will not match)

Traceback (most recent call last):
File "P:\Burke\TRACKERS\Ortho-McNeil\Automation\Wave3\test.py", line 53, in ?
VAR = re.search("PROVEN.*?\n[\sA-Za-z\(\)\/\-]+\d(.*?)\n.*?" , pcpT9, re.S ).group(1) #.split()[1]
AttributeError: 'NoneType' object has no attribute 'group'
Is there a way in python to make this "non" match equal a value (say, "0")?

This one line is actually a series of searches

Reasons = ["EFFICACY","EFFECTIVE","WORKS QUICKLY","PROVEN","SAFETY","LITTLE","WELL"," BETTER","SAMPLES"] # Reasons(29)
VAR = [re.search(r"%s.*?\n[\sA-Za-z\(\)\/\-]+\d(.*?)\n.*?" % i, pcpT9, re.S ).group(1) for i in Reasons ] #.split()[1]

and one of the items in the array may or may not be present.

Lance
Feb 15 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Lance Hoffmeyer wrote:
I have a search:

VAR = re.search("PROVEN.*?\n[\sA-Za-z\(\)\/\-]+\d(.*?)\n.*?" , pcpT9, re.S
).group(1) #.split()[1]

that does not match (sometimes it will and sometimes it will not match)

Traceback (most recent call last):
File "P:\Burke\TRACKERS\Ortho-McNeil\Automation\Wave3\test.py", line 53,
in ?
VAR = re.search("PROVEN.*?\n[\sA-Za-z\(\)\/\-]+\d(.*?)\n.*?" , pcpT9,
re.S ).group(1) #.split()[1]
AttributeError: 'NoneType' object has no attribute 'group'
Is there a way in python to make this "non" match equal a value (say,
"0")?
match = re.search(...)
if match:
value = match.group(1)
else:
value = default

Wrap that into a function and you can use it inside a list comprehension.
This one line is actually a series of searches

Reasons = ["EFFICACY","EFFECTIVE","WORKS
QUICKLY","PROVEN","SAFETY","LITTLE","WELL"," BETTER","SAMPLES"] #
Reasons(29)
VAR = [re.search(r"%s.*?\n[\sA-Za-z\(\)\/\-]+\d(.*?)\n.*?" % i, pcpT9,
re.S ).group(1) for i in Reasons ] #.split()[1]

and one of the items in the array may or may not be present.
How about a slightly different approach (untested):

r = re.compile(r"(%s).*?\n[\sA-Za-z\(\)\/\-]+\d(.*?)\n.*?" %
"|".join(reasons), re.S)
groups = dict(m.groups() for m in r.finditer(pcpT9))
VAR = [groups.get(reason, "0") for reason in reasons]

I recommend that you don't create the VAR list and use groups directly.
If you are using Python 2.5 you can replace the builtin dict with a
collections.defaultdict.

Peter
Feb 15 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.