[tpm] New Regex question

Rob Janes janes.rob at gmail.com
Sat Oct 27 17:59:38 PDT 2012


this is a one liner ...

$exp =~ s/(\s|\r\n)\1+/\1/g;

you should replace the * with a +
otherwise you're wasting time replacing something with itself.  the regex
matches single blanks, which are replaced with single blan

On Sat, Oct 27, 2012 at 8:53 PM, Chris Jones <cj at enersave.ca> wrote:

>
> The following two regex work.
> Expression before:
> if(Local(TYPE) == 1) then
>   1.25
>
> else
>   UNCHANGED
> endif
>
> Run script.
> $expression =~ s/(\s)\1*/\1/g;
> $expression  =~ s/(\r\n)+/\r\n/g;
>
> Expression after:
> if(Local(TYPE) == 1) then
>  1.25
> else
>  UNCHANGED
> endif
>
> There is likely a way to make that one expression but it is working for my
> needs.
>
>
>
>
> At 08:18 PM 27/10/2012, Chris Jones wrote:
>
>> I am reading a memo field from an access database so the carriage
>> return/line feeds are in the memo field. I am not sure what character is
>> used but \n doesn't seem to be correct.
>>
>>
>>
>>
>>
>> At 06:45 PM 27/10/2012, Tom Legrady wrote:
>>
>>> Your problem is that you're stuffing back to much, in the 'replace' have
>>> of the s//. You just want to stick in one space. \1 is ALL the spaces you
>>> found; and \s is only special in the search half; in the replace half it's
>>> just the letter 's'.
>>>
>>> my $SPACE = q{ };
>>> $expression =~ s/(\s+)/$SPACE/g;
>>>
>>> To deal with empty lines, you can search for multiple newlines with
>>> nothing but optional spaces or tabs in between:
>>>
>>> $lines =~ s/\n[ \t]*\n/\n/
>>>
>>> Personally, I'm more likely to process input files line by line, so I
>>> would simply skip "empty" lines.
>>>
>>> LINE:
>>> while ( my $line = <$fh> ) {
>>>    next LINE if $line =~ m{^\s*$}m;
>>>    chomp $line;
>>>    ...
>>> }
>>>
>>
>> >>
>> 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>
>>
>
> >>
> 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/26c6a6a9/attachment.html>


More information about the toronto-pm mailing list