[tpm] New Regex question

Rob Janes janes.rob at gmail.com
Sat Oct 27 15:37:30 PDT 2012


$expression =~ s/(\s)\s+/\1/gs

that replaces sequences of two or more \s with one of whatever the first
one matched.
note that mixed tabs and blanks will get replaced.

if you just want replacement of pure strings of a particular character ...

$expression =~ s/(\s)\1+/\1/gs

this will replace strings of blanks with a single blank, but if the blanks
and tabs alternate it will do nothing with it.

if you load $expression with multiple lines, this should also reduce
multiple lines to one.  don't try this on windows files though.

cheers,
Rob

On Sat, Oct 27, 2012 at 6:19 PM, Chris Jones <cj at enersave.ca> 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;
> That gave me the original double white space plus an s.
>
> Any insight very much appreciated.
>
>
> But that replaces all white spaces with
> At 07:55 PM 23/10/2012, Stuart Watt wrote:
>
>> On 2012-10-23, at 2:32 PM, Chris Jones <cj at enersave.ca> wrote:
>>
>> > My tests are working correctly, I don't try to delete directories.
>> >
>> > My follow on question is how do you tell File:Find to skip the
>> directory the script is called from.  I only want to delete files in the
>> sub-directories.
>> >
>> > I can test each file to see if it's directory is '.' but would seem
>> somewhat inefficient?
>>
>> String matching efficiency is probably a thousand times higher than file
>> system accesses, so if I was you I really wouldn't worry about it. I tend
>> to use no_chdir => 1 and $File::Find::name, but to skip everything in the
>> base directory, probably checking to see if $File::Find::dir is the same as
>> the base should be quick and effective.
>>
>> All the best
>> Stuart
>> ______________________________**_________________
>>
>
> >>
> Christopher Jones, P.Eng.
> Suite 1801, 1 Yonge Street
> Toronto, ON M5E1W7
> Tel. 416-203-7465
> Fax. 416-946-1005
> email cj at enersave.ca
>
> ______________________________**_________________
> 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/8ac40536/attachment.html>


More information about the toronto-pm mailing list