[tpm] New Regex question

Chris Jones cj at enersave.ca
Sat Oct 27 17:53:49 PDT 2012


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

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



More information about the toronto-pm mailing list