Correct me if I'm wrong, but your regex is saying "match dog, unless it's followed by cat. if it is followed by cat there is no match on this side of the "|" at which point we advance past it and look at the alternative expression which says to match in front of cat."
However, if I run a .sub using your regex on a string contain both dog and cat, both will be replaced.
A simple example will show what I mean:
>>> import re >>> x = re.compile(r"(A) | (B)") >>> s = "X R A Y B E" >>> r = x.sub("13", s) >>> print r X R 13Y13 E
...so unless I'm understanding it wrong, "B" is supposed to be ignored if "A" is matched, yet I get both matched. I get the same result if I put "A" and "B" within the same group.
On Mar 8, 2005, at 6:47 PM, Danny Yoo wrote:
Regular expressions are a little evil at times; here's what I think you're
thinking of:
###... | (?<=dogcat)""", re.VERBOSE)import re pattern = re.compile(r"""dog(?!cat)0pattern.match('dogman').start()pattern.search('dogcatcher').start()
Hi Mike,
Gaaah, bad copy-and-paste. The example with 'dogcatcher' actually does come up with a result:
###pattern.search('dogcatcher').start()6 ###
Sorry about that!
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor