From: 7stud 7stud Date: 2007-03-14T16:54:23+09:00 Subject: ruby regex lookarounds? In Programming Ruby(1st Ed), it says: -------- The match operators return the character position at which the match occurred. They also have the side effect of setting a whole load of Ruby variables. $& receives the part of the string that was matched by the pattern, $` receives the part of the string that preceded the match, and $' receives the string after the match... The match also sets the thread-global variables $~ and $1 through $9. The variable $~ is a MatchData object (described beginning on page 336) that holds everything you might want to know about the match. $1 and so on hold the values of parts of the match. ------- Here is an example: irb(main):009:0> "abc" =~ /(a)(b)/ => 0 irb(main):010:0> $& => "ab" irb(main):011:0> $1 => "a" irb(main):012:0> $2 => "b" Now and example with a lookbehind: irb(main):013:0> "abc" =~ /(?:ab)(c)/ => 0 irb(main):014:0> $& => "abc" irb(main):015:0> $1 => "c" Isn't that output inconsistent? If Ruby wants to say that the regex matches the whole string, then shouldn't $1 be 'ab'. I know that lookarounds shouldn't be considered part of the match--they are just assertions, but Ruby's $& variable doesn't seem to respect that. -- Posted via http://www.ruby-forum.com/.