[tpm] New Regex question
Rob Janes
janes.rob at gmail.com
Sat Oct 27 17:52:28 PDT 2012
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.
oh - perl 5.14.2.
-rob
On Sat, Oct 27, 2012 at 7:37 PM, Rob Janes <janes.rob at gmail.com> wrote:
> That's what I thought.
>
> Until I tried it. The second use case does not work with $1.
>
> Try it.
> On Oct 27, 2012 7:22 PM, "Uri Guttman" <uri at stemsystems.com> wrote:
>
>> On 10/27/2012 06:57 PM, Rob Janes wrote:
>>
>>> $1 doesn't work.
>>>
>>> \1 is the characters matched while $1 is the pattern matched.
>>>
>>> It only matters if u want to match pure string.
>>>
>>
>> that is wrong. both are only references to a grab which is always a
>> string. you can use \1 in the regex to match what you have previously
>> matched and grabbed. $1 is used in the replacement or after the regex to
>> get what was grabbed. neither contains a regex.
>>
>> uri
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20121027/bbf0d41a/attachment.html>
More information about the toronto-pm
mailing list