[tpm] New Regex question

Rob Janes janes.rob at gmail.com
Sat Oct 27 15:57:18 PDT 2012


$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.

Btw careful with the \n matching.
On Oct 27, 2012 6:51 PM, "Uri Guttman" <uri at stemsystems.com> wrote:

> On 10/27/2012 06:19 PM, Chris Jones wrote:
>
>> Thank Stuart, I did end up using $File::Find::dir, very helpful.
>>
>> New question.  I would like to replace multiple spaces with single
>> spaces and multiple end of line  with a single end of line.
>>
>> I thought this might work:
>> $expression =~ s/(\s+)/\1/g; # puts back what it found
>>
>> So I tried
>> $expression =~ s/(\s)/\1\s/g;
>>
>
> someone else gave you a decent solution but i want to clear up a
> misconception you have. the right side of s/// is a replacement string and
> \s is only a regex shortcut for white space. putting \s there will only
> give you a 's' char in the replacement. also \1 is not meant to be used in
> the replacement but only in the regex. $1 is the correct thing in the
> replacement. \1 is supported for backwards compatibility.
>
> another and likely better solution is to use tr///. this code will replace
> any run of space, \t, \r\n with a single one of each.
>
>         tr/ \t\\n\r//s
>
> the replacement pattern will be duplicated from the left side and the /s
> modifier will squeeze them to 1 char. if you want to make any \r or \n runs
> be squeezed to a single \n you need an explicit replacement part:
>
>         tr{ \t\n\r}{ \t\n\n}s
>
> i switched the delims as there were too many /\ leaning toothpicks.
>
> uri
>
> ______________________________**_________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/**listinfo/toronto-pm<http://mail.pm.org/mailman/listinfo/toronto-pm>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20121027/e6954e8c/attachment-0001.html>


More information about the toronto-pm mailing list