[tpm] New Regex question

Uri Guttman uri at stemsystems.com
Sat Oct 27 20:56:12 PDT 2012

On 10/27/2012 08:52 PM, Rob Janes wrote:
> sounds a bit muddy ...
> just to clarify, what I found to my surprise,  is that
> $exp =~ s/(\s)$1+/$1/g;
> replaces mixed strings of white space with the first character.  leading me
> to conclude that the first $1 actually is the \s, not the specific
> character matched.  However, in the second part of the s, the $1 did indeed
> give the white space.  So the $1 in the context of searching appears to
> render as the regex, not the characters matched.  While in the context of
> replacing, the $1 renders as the actual characters matched, not the regex
> used.
> hope that makes it clearer.

sorry but no.

$1 in the regex is what $1 was BEFORE the regex. it gets interpolated 
and then parsed as a regex. it is not related to the () in the regex. 
only \1 will be the grabbed text from that (). and $1 in the replacement 
is just the string matched in the () which is any single whitespace char.


More information about the toronto-pm mailing list