Using re module better

M

Mike

I seem to fall into this trap (maybe my Perl background) where I want
to simultaneously test a regular expression and then extract its match
groups in the same action. For instance:

if (match = re.search('(\w+)\s*(\w+)', foo)):
field1 = match.group(1)
field2 = match.group(2)
...

(compare to Perl:)

if($foo =~ /(\w+)\s*(\w+)/) {
$field1 = $1;
$field2 = $2;
...
}

Problem is, my python is invalid above. What's the pythonic way to do
this?

Thanks in advance O Python Charmers

Mike
 
T

Tim Chase

if (match = re.search('(\w+)\s*(\w+)', foo)):

Caveat #1: use a raw string here
Caveat #2: inline assignment is verboten

match = re.search(r'(\w+)\s*(\w*+)', foo)
if match:
field1 = match.group(1)
field2 = match.group(2)

This should then work more or less. However, since you know
there are two matches, you can just use

field1, field2 = match.groups()


If the regexp is one you plan to reuse (or call in a loop), you
can pre-compile it:

r = re.compile(r'(\w+)\s*(\w*+)')
for thing in bunch_of_things:
m = r.search(thing)
if m:
field1, field2 = m.groups()
do_something(field1, field2)

HTH,

-tkc
 
B

Bruno Desthuilliers

Mike a écrit :
I seem to fall into this trap (maybe my Perl background) where I want
to simultaneously test a regular expression and then extract its match
groups in the same action.
For instance:

if (match = re.search('(\w+)\s*(\w+)', foo)):
field1 = match.group(1)
field2 = match.group(2)
...

(compare to Perl:)

if($foo =~ /(\w+)\s*(\w+)/) {
$field1 = $1;
$field2 = $2;
...
}

Problem is, my python is invalid above. What's the pythonic way to do
this?

In your above use case, the solution is obvious:

match = re.search('(\w+)\s*(\w+)', foo)
if match:
field1 = match.group(1)
field2 = match.group(2)


wrt/ assignement as an expression, this has been discussed about 2 days
ago - look for a thread (badly) named "Is it possible to return a
variable and use it...?"

HTH
 
C

castironpi

Caveat #1:  use a raw string here
Caveat #2:  inline assignment is verboten

   match = re.search(r'(\w+)\s*(\w*+)', foo)
   if match:


This should then work more or less.  However, since you know
there are two matches, you can just use

   field1, field2 = match.groups()

If the regexp is one you plan to reuse (or call in a loop), you
can pre-compile it:

   r = re.compile(r'(\w+)\s*(\w*+)')
   for thing in bunch_of_things:
     m = r.search(thing)
     if m:
       field1, field2 = m.groups()
       do_something(field1, field2)

HTH,

-tkc

Opposed is to mimic MatchGroup that doesn't do anything, and returns
from a non-match call, field1, field2 = match.groups() or [ Dummy,
Dummy ], and similarly ...= [ None, None ].

On another note, why do people X, Y, and Z, including me, all hate to
write a= b(); if a: a.something()?
 
S

Steven D'Aprano

On another note, why do people X, Y, and Z, including me, all hate to
write a= b(); if a: a.something()?


Is this a guessing game? You want us to guess why YOU hate to do
something? Okay, I'm game for guessing games...

You were traumatized as a child by a newline byte, and now you try to
write everything possible as a one-liner.

Am I close?

Speaking for myself, I *prefer* to separate the assignment from the
comparison. I dislike greatly the syntax "if (a=b()): a.something()".
 

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,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top