From: David Alan Black Date: 2004-05-12T09:53:51+09:00 Subject: Re: Strange regexp behaviour in gsub Hi -- Kristof Bastiaensen writes: > On Wed, 12 May 2004 08:15:28 +0900, Joel VanderWerf wrote: > > > Kristof Bastiaensen wrote: > >> Hi, > >> > >> I am wondering if this is the correct behaviour in gsub: > >> > >> "bab".gsub(/(?!a)ab/, "cd") > >> => "bab" > >> > >> shouldn't that be "bcd"? > > > > I think /(?!a)ab/ can't match anything. It's saying that the first > > character after the beginning of the match must not be "a", and the > > first character of the match must be "a". This is contradictory. > > Yes, that would clarify the situation, but is it the correct > behaviour? I would think that (?!a)a doesn't mean the same > character, but consecutive ones. Because it doesn't consume the > character, it effectively is the character 'before' the match (if > any). /(?!a)/ doesn't match or consume any character; it refers to the state of things between characters. The previous character (or start of string) has come and gone; the assertion, now, is "what lies just ahead is not 'a'". > The other behaviour wouldn't make sense, because (?!a)b is then > exactly the same as b. Assertions like this always have the possibility of being redundant -- for example: /(?=a)abc/ # same as /abc/ but there are a lot of cases where they aren't, and that's where they become useful: /David (?!Black)(\S+)/ # grab another David's last name David -- David A. Black dblack@wobblini.net