--------
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:
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.
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.
I see what's going on. (?:ab) is not a grouping: (?: and ) do not form a
grouping that gets a $ variable. If I write it as (?:(ab)), then ab is
a grouping:
The syntax (?:re) isn't a lookbehind. It is a grouping form like (re)
but without capture. So (?:(re)) is the same as (re). Off course, you
could write something like (?:a(bc)), and you will get "bc" in $1, and
not "abc".
···
On 14 mar, 09:35, 7stud 7stud <dol...@excite.com> wrote:
I see what's going on. (?:ab) is not a grouping: (?: and ) do not form a
grouping that gets a $ variable. If I write it as (?:(ab)), then ab is
a grouping: