From: Lars Christensen Date: 2002-05-08T02:10:58+09:00 Subject: Re: RegExp zero-width negative lookahead On Wed, 8 May 2002 nobu.nokada@softhome.net wrote: > Hi, > > At Tue, 7 May 2002 23:33:50 +0900, > Lars Christensen wrote: > > I'm puzzled why this doesn't work the way I expect it to. > > > > "xxoxxxoxx" =~ /(?!x)ox+o(?!x)/ #=> nil (ok) > > "xx oxxxoxx" =~ /(?!x)ox+o(?!x)/ #=> nil (ok) > > "xx oxxxo xx" =~ /(?!x)ox+o(?!x)/ #=> 3 (ok) > > "xxoxxxo xx" =~ /(?!x)ox+o(?!x)/ #=> 2 (huh!?) > > > > Why is the string matched in the last case? > > Already pointed out the reason, however you really want look > behind? > > "xxoxxxo xx" =~ /(? nil (with Oniguruma) Yes, this is was what I am missing. My current work-around is /([^x]|^)ox+o(?!x)/, but then I need to prepend $1 to the replacement, e.g. str.gsub(/([^x]|^)ox+o(?!x)/) { $1 + "replacement" } .. and it gets even worse/impossible if one wants to non-match multiple characters. btw; Thanks to all for the explanations! -- Lars Christensen