[tpm] New Regex question

Chris Jones cj at enersave.ca
Sat Oct 27 17:18:50 PDT 2012

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

More information about the toronto-pm mailing list