Re: reg exp
From: Gunnar Hjalmarsson (noreply_at_gunnar.cc)
Date: 08/30/04
- Previous message: Ken Chesak: "reg exp"
- In reply to: Ken Chesak: "reg exp"
- Next in thread: Joe Smith: "Re: reg exp"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 30 Aug 2004 17:15:54 GMT
Ken Chesak wrote:
> Perl scipt is formatting text for HTML page. It changes things like
> an & to &. But should not change  . It uses \ as an escape
> character. So \  will become  . The final results are
> correct, but is there a better way to do this?
>
> Input file test.txt
> \HOME & \ BORN \& FREE BORN FREE ' \' HELP " \" w\\\\\\\w
>
> 1st change
> 1a= \HOME & \ BORN \& FREE BORN FREE '' \' HELP " \"
> w\\\\\\\w
> 2nd changes
> 1b= HOME & BORN & FREE BORN FREE '' ' HELP " "
> w\\\w
>
> #!/usr/local/bin/perl5
> #
> %encode = ( '&' => '&',
> '"' => '"',
> '\'' => '\'\'' );
>
> $data = `cat test.txt`;
> print "Oa= $data\n";
> $data =~ s/(?<!\\)(.)/defined($encode{$1})?$encode{$1}:$1/eg;
> print "1a= $data\n";
> $data =~ s/(\\)(.)/$2/g;
> print "1b= $data\n";
Don't know about better, but this does it with one substitution, and
does not require escaping of HTML entities in the original text:
$data =~ s{(&#?\w+;)|\\(.)|([&"'])}
{ $1 ? $1 : $2 ? $2 : $encode{$3} }eg;
Another thing is that I'm a bit confused about the wider purpose with
the exercise...
-- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl
- Previous message: Ken Chesak: "reg exp"
- In reply to: Ken Chesak: "reg exp"
- Next in thread: Joe Smith: "Re: reg exp"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|