From: Rob Williscroft on
samb wrote in news:5c361012-1f7b-487f-915b-0f564b238be3
@e1g2000yqh.googlegroups.com in comp.lang.python:

> Thanks for all those suggestions.
> They are good!
>
> 1) Let's suppose now that instead of just affecting "thing =
> m.group(1)", I need to do a piece of logic depending on which match I
> entered...
>
> 2) Concerning the suggestion :
> m = re.match(r'define\s+(\S+)\s*{$', line)
> if m:
> thing = m.group(1)
>
> m = re.match(r'include\s+(\S+)$', line)
> if m:
> thing = m.group(1)
>
> #etc...
>
> It means that I'll do all the checks, even if the first one did match
> and I know that the next will not...
>

Ths is how I did it when I had the need:

class ReMatch( object ):
def __call__( self, pat, string ):
import re
self.match = re.match( pat, string )
return self.match is not None

clip = ...

re = ReMatch()

if re( r'\s*TM(\d+)', clip ):
...
elif re( r'\s*(https?://.*)', clip ):
...
elif re( r'\d{12}$', clip ):
...

Rob.
--
From: Chris Rebert on
On Tue, Mar 16, 2010 at 1:37 AM, samb <sam.bancal(a)gmail.com> wrote:
> Thanks for all those suggestions.
> They are good!
<snip>
> 2) Concerning the suggestion :
> m = re.match(r'define\s+(\S+)\s*{$', line)
> if m:
>    thing = m.group(1)
>
> m = re.match(r'include\s+(\S+)$', line)
> if m:
>    thing = m.group(1)
>
> #etc...
>
> It means that I'll do all the checks, even if the first one did match
> and I know that the next will not...

Note how I split it out into a separate function and used `return
m.group(1)` to avoid that exact situation.

Cheers,
Chris
--
http://blog.rebertia.com
From: samb on
On Mar 16, 9:53 am, Chris Rebert <c...(a)rebertia.com> wrote:
> On Tue, Mar 16, 2010 at 1:37 AM, samb <sam.ban...(a)gmail.com> wrote:
> > Thanks for all those suggestions.
> > They are good!
> <snip>
> > 2) Concerning the suggestion :
> > m = re.match(r'define\s+(\S+)\s*{$', line)
> > if m:
> >    thing = m.group(1)
>
> > m = re.match(r'include\s+(\S+)$', line)
> > if m:
> >    thing = m.group(1)
>
> > #etc...
>
> Note how I split it out into a separate function and used `return
> m.group(1)` to avoid that exact situation.

Yes, you're right.
It's an interresting approach. I'll give it a try.

Cheers
From: samb on
Hi,

I've found a work around, inspired from Rob Williscroft :

class ReMatch(object):
"""
Object to be called :
1st time : do a regexp.match and return the answer (args:
regexp, line)
2nd time : return the previous result (args: prev)
"""
def __call__(self, regexp='', line='', prev=False):
if prev:
return self.prev_match
self.prev_match = re.match(regexp, line)
return self.prev_match

re_match = ReMatch()

if re_match(r'define\s+(\S+)\s*{$', line):
m = re_match(prev=True)
# do some logic with m
elif re_match(r'include\s+(\S+)$', line):
m = re_match(prev=True)
# do some logic with m
else
# do some logic

Hope this is efficient ... I guess yes.

Cheers,
Sam
From: Peter Otten on
samb wrote:

> I've found a work around, inspired from Rob Williscroft :
>
> class ReMatch(object):
> """
> Object to be called :
> 1st time : do a regexp.match and return the answer (args:
> regexp, line)
> 2nd time : return the previous result (args: prev)
> """
> def __call__(self, regexp='', line='', prev=False):
> if prev:
> return self.prev_match
> self.prev_match = re.match(regexp, line)
> return self.prev_match
>
> re_match = ReMatch()
>
> if re_match(r'define\s+(\S+)\s*{$', line):
> m = re_match(prev=True)
> # do some logic with m
> elif re_match(r'include\s+(\S+)$', line):
> m = re_match(prev=True)
> # do some logic with m
> else
> # do some logic
>
> Hope this is efficient ... I guess yes.

No; just accessing the prev_match attribute instead of passing a flag to the
__call__() method is more efficient and easier to read. I think the latter
is the relevant point...

Peter