YANRQ (yet another regex question)

Mats Kindahl matkin at iar.se
Mon Mar 18 07:16:18 EST 2002


Michael George Lerner <mlerner at rastan.gpcc.itd.umich.edu> writes:

> Mats Kindahl <matkin at iar.se> wrote:
> > Mats Kindahl <matkin at iar.se> writes:
> 
> >> Michael George Lerner <mlerner at asteroids.gpcc.itd.umich.edu> writes:
> >> 
> >> > I wanted to match this:
> >> > 
> >> > (?P<foo>foo    | foo   |  foo  |   foo |    foo)
> >> > 
> 
> <snip>
> 
> >> That's what I get for not reading the manual... :/
> 
> <snip>
> 
> >  
> >      r = re.compile(r'(?P<foo>(?=.{7}$) *foo *$)')
> >                                      ^        ^
> >                                      !        !
> >             notice missing dollars --+--------+
> 
> Cool! .. but still not quite what I wanted ..
> 
> And that's what *I* get for snipping information out when I post a
> followup :/.  
> 
> In my original post, I mentioned that I wanted something like 
> r'(?P<start>start)(?P<foo>  foo  )(?P<end>end)' i.e. other patterns 
> on the same line before and after foo.

I might be misunderstanding something, but can't you just do...

    r = re.compile(r'(?P<start>start)'
                   + r'(?P<foo>(?=.{7}end) *foo *)'
                   + r'(?P<end>end)')

...but now it's starting to be complicated. (Maybe you should use the
VERBOSE flag when compiling the pattern.)

Notice that you have to insert the end marker/pattern 'end' both
inside the lookahead pattern and after the actual match pattern.

Observe that for this to work, the 'start' pattern may not be a legal
prefix to the 'foo' pattern (this is not formally correct, but I
believe you get the meaning) nor may the 'end' pattern may not be a
legal suffix of the 'foo' pattern.

Good luck!

-- 
Mats Kindahl, IAR Systems, Sweden

Any opinions expressed are my own, not my company's.



More information about the Python-list mailing list