Shortcut needed: avoiding temporary variables with regular expressions

Pekka Niiranen pekka.niiranen at wlanmail.com
Sat Nov 6 16:58:26 EST 2004


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-



More information about the Python-list mailing list