[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Using re module better

Mike

3/5/2008 11:33:00 AM

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
7 Answers

Tim Chase

3/5/2008 12:13:00 PM

0

> 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



Bruno Desthuilliers

3/5/2008 12:13:00 PM

0

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

Aaron Brady

3/5/2008 7:09:00 PM

0

On Mar 5, 6:12 am, Tim Chase <python.l...@tim.thechases.com> wrote:
> > 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

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()?

Steven D'Aprano

3/5/2008 10:07:00 PM

0

On Wed, 05 Mar 2008 11:09:28 -0800, castironpi wrote:

> 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()".


--
Steven

homebrood

2/17/2011 9:09:00 PM

0

On Feb 17, 2:21 pm, Dean Seavers <uvk...@gmail.com> wrote:
> On Feb 17, 12:09 pm, Adm56 <adm56h...@yahoo.com> wrote:
>
> > On Feb 17, 11:56 am, Mike <kahl...@yahoo.com> wrote:
>
> > > I know the 60's produced plenty of questionable artwork....but now I
> > > see some wacko cabinets too:
>
> > >http://ipdb.org/showpic.pl?id=1178&p...
>
> > >http://ipdb.org/showpic.pl?id=641&...
>
> > > what were they thinking?  Looks like a cross between a kitchen table
> > > and a cigarette vending machine.
>
> > That's what they thought the future would lool like in the 60's...
>
> I think those cabinets are pretty cool, actually! I have a Williams
> Jungle and that has the Jestsons cabinet as well. I think anything
> that is pop culture-related is going to look dated and of its time.
> You can look at any pinball machine and instantly tell what era it's
> from. So it's a matter of what time period appeals to you
> aesthetically. I think those Williams "Styling of the 60's" cabinets
> capture the whole late50's/early 60's atomic age vibe perfectly - and
> it happens to be an era I find aesthetically interesting. It's kind of
> the '59 Cadillac of pinball cabinets.

Has a great shelf there to put your beer and snacks!

I love those games, they are retro weird cool!

Tom

mike

2/17/2011 9:33:00 PM

0

On Feb 17, 1:08 pm, homebrood <homebr...@aol.com> wrote:
> On Feb 17, 2:21 pm, Dean Seavers <uvk...@gmail.com> wrote:
>
>
>
>
>
> > On Feb 17, 12:09 pm, Adm56 <adm56h...@yahoo.com> wrote:
>
> > > On Feb 17, 11:56 am, Mike <kahl...@yahoo.com> wrote:
>
> > > > I know the 60's produced plenty of questionable artwork....but now I
> > > > see some wacko cabinets too:
>
> > > >http://ipdb.org/showpic.pl?id=1178&p...
>
> > > >http://ipdb.org/showpic.pl?id=641&...
>
> > > > what were they thinking?  Looks like a cross between a kitchen table
> > > > and a cigarette vending machine.
>
> > > That's what they thought the future would lool like in the 60's...
>
> > I think those cabinets are pretty cool, actually! I have a Williams
> > Jungle and that has the Jestsons cabinet as well. I think anything
> > that is pop culture-related is going to look dated and of its time.
> > You can look at any pinball machine and instantly tell what era it's
> > from. So it's a matter of what time period appeals to you
> > aesthetically. I think those Williams "Styling of the 60's" cabinets
> > capture the whole late50's/early 60's atomic age vibe perfectly - and
> > it happens to be an era I find aesthetically interesting. It's kind of
> > the '59 Cadillac of pinball cabinets.
>
> Has a great shelf there to put your beer and snacks!
>
> I love those games, they are retro weird cool!
>
> Tom- Hide quoted text -
>
> - Show quoted text -

Does every pin have to look cookie cutter? Those legs rock! The
cabinet looks cool also. It looked space aged for its time. Don't be
SS snob.

Ceegary

2/18/2011 4:55:00 AM

0

On Feb 17, 2:33 pm, mike <wizb...@gmail.com> wrote:
> On Feb 17, 1:08 pm, homebrood <homebr...@aol.com> wrote:
>
>
>
>
>
> > On Feb 17, 2:21 pm, Dean Seavers <uvk...@gmail.com> wrote:
>
> > > On Feb 17, 12:09 pm, Adm56 <adm56h...@yahoo.com> wrote:
>
> > > > On Feb 17, 11:56 am, Mike <kahl...@yahoo.com> wrote:
>
> > > > > I know the 60's produced plenty of questionable artwork....but now I
> > > > > see some wacko cabinets too:
>
> > > > >http://ipdb.org/showpic.pl?id=1178&p...
>
> > > > >http://ipdb.org/showpic.pl?id=641&...
>
> > > > > what were they thinking?  Looks like a cross between a kitchen table
> > > > > and a cigarette vending machine.
>
> > > > That's what they thought the future would lool like in the 60's...
>
> > > I think those cabinets are pretty cool, actually! I have a Williams
> > > Jungle and that has the Jestsons cabinet as well. I think anything
> > > that is pop culture-related is going to look dated and of its time.
> > > You can look at any pinball machine and instantly tell what era it's
> > > from. So it's a matter of what time period appeals to you
> > > aesthetically. I think those Williams "Styling of the 60's" cabinets
> > > capture the whole late50's/early 60's atomic age vibe perfectly - and
> > > it happens to be an era I find aesthetically interesting. It's kind of
> > > the '59 Cadillac of pinball cabinets.
>
> > Has a great shelf there to put your beer and snacks!
>
> > I love those games, they are retro weird cool!
>
> > Tom- Hide quoted text -
>
> > - Show quoted text -
>
> Does every pin have to look cookie cutter? Those legs rock! The
> cabinet looks cool also. It looked space aged for its time. Don't be
> SS snob.- Hide quoted text -
>
> - Show quoted text -

Absolutely cool. Especially when pins were everywhere, can you imagine
how many beers and sodas were spilled into a game? A place for a
drink, an ashtray and some french fries with the space age look, go to
love it.

GRY