[Tutor] regular expression question
Mike Hall
michael.hall at critterpixstudios.com
Wed Mar 9 03:40:48 CET 2005
This will match the position in front of "dog":
(?<=dog)
This will match the position in front of "cat":
(?<=cat)
This will not match in front of "dog" if "dog" is followed by "cat":
(?<=dog)\b (?!cat)
Now my question is how to get this:
(?<=cat)
...but ONLY if "cat" is following "dog." If "dog" does not have "cat"
following it, then I simply want this:
(?<=dog)
...if that makes sense :) thanks.
On Mar 8, 2005, at 6:05 PM, Danny Yoo wrote:
>
>
> 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