re.match question

Thomas Svensson thomas.svensson at era.ericsson.se
Mon Oct 9 08:54:09 EDT 2000


Dan Schmidt wrote:
> 
> Thomas Svensson <thomas.svensson at era.ericsson.se> writes:
> 
> | Hello,
> |
> | I'm matching a list of strings. I want "xxxBar" to be a match but not
> | "xxxFooBar", xxx can be anything. I've tried the following:
[...]

> Here's a nasty way that seems to work:
> 
>   re.match ('^.*(?!Foo)...Bar$', word)
> 
> This way we require three characters before Bar, and before consuming
> them, make sure that they're not Foo.  [Oops: this fails for 'xxBar'!
> See below.]

Thanks!

This works for me, fortunately my xxx is always long enough. Guess my
brain is not very good at compiling regexps.

> You could also do something like '^.*(...)Bar$' and then check what
> you captured inside (...) afterwards.
> 
> Or do '^.*Bar$', use the start method of the Match object that's
> returned to find out what index Bar is at, and check the substring of
> three characters before it.
> 
> Trying to do it completely within the regexp language is probably the
> messiest choice.

In my case I have a list of several different regexps which are used on
the same list of strings. Hence, I want my regexps to be generic and not
require extra checks. I never thought it be that complicated. Lookbind
assertion seems to be what I need but I'm using JPython so this solution
will have to do for now.

Anyway, thanks again.

/ Thomas



More information about the Python-list mailing list