[Tutor] regular expression question

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Wed Mar 9 03:05:36 CET 2005



On Tue, 8 Mar 2005, Mike Hall wrote:

> I'd like to get a match for a position in a string preceded by a
> specified word (let's call it "Dog"), unless that spot in the string
> (after "Dog") is directly followed by a specific word(let's say "Cat"),
> in which case I want my match to occur directly after "Cat", and not
> "Dog."

Hi Mike,

You may want to look at "lookahead" assertions.  These are patterns of the
form '(?=...)' or '(?!...).  The documentation mentions them here:

   http://www.python.org/doc/lib/re-syntax.html

and AMK's excellent "Regular Expression HOWTO" covers how one might use
them:

http://www.amk.ca/python/howto/regex/regex.html#SECTION000540000000000000000



More information about the Tutor mailing list