re.search making no match == 0

Peter Otten __peter__ at web.de
Thu Feb 15 12:58:30 EST 2007


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



More information about the Python-list mailing list