Shortcut needed: avoiding temporary variables with regular expressions

P

Pekka Niiranen

Hi there,

I am using regular expressions like this:

matcher = re.compile(r'(.*)\s(.*)', re.UNICODE)
tmp = matcher.search(mystring)
if tmp:
myvariable = tmp.group(1)

The idea is that group(1) is accessed only if
it was found from 'mystring'. Is there a way
to avoid the usage of 'tmp' -variable?
Sure, I could try:

matcher = re.compile(r'(.*)\s(.*)', re.UNICODE)
if matcher.search(mystring):
myvariable = matcher.search(mystring).group(1)

but then I am searching 'mystring' twice.

-pekka-
 
A

Alex Martelli

Pekka Niiranen said:
Hi there,

I am using regular expressions like this:

matcher = re.compile(r'(.*)\s(.*)', re.UNICODE)
tmp = matcher.search(mystring)
if tmp:
myvariable = tmp.group(1)

The idea is that group(1) is accessed only if
it was found from 'mystring'. Is there a way
to avoid the usage of 'tmp' -variable?

Yeah, quite a few recipes on the cookbook could help, starting with one
of mine, many years old now, about how to do assign-and-test together in
Python (the only use case being to transcribe faithfully an algorithm
coming from languages that focus on assign-and-test, such as Perl or C;
once you move the algorithm fully to Python, it's best to use Python
idioms, of course).

In my old recipe,, you need to have somewhere an auxiliary class and
instance thereof, such as:

class Data(object):
def set(self, value):
self.value = value
return value
d = Data()

then, you can do:

if d.set(matcher.search(mystring)):
myvariable = d.value.group(1)

This is very general and generic. For your specific case, you could do:

try: myvariable = matcher.search(mystring).group(1)
except (AttributeError, TypeError): pass

i.e., just try to access .group even if the search results in None, and
simply catch the error that happens when you do (I think it will be an
AttributeError, but I'm not sure that holds in all versions you care
about, so I threw in a TypeError for good measure;-).


Alex
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top