re.search making no match == 0

L

Lance Hoffmeyer

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
 
P

Peter Otten

Lance said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top