[tpm] New Regex question
Uri Guttman
uri at stemsystems.com
Sat Oct 27 15:51:29 PDT 2012
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
More information about the toronto-pm
mailing list