in reply to Another Regular Expression

I recommend the HTML::Entities module for that sort of job.

$ perl -MHTML::Entities -e'print encode_entities(q(I have a 15" latter + and a 6" foot arm.))' I have a 15" latter and a 6" foot arm.$
That doesn't cover omitting leading and trailing quotes, so lets cook up a regex and a substitution,
use HTML::Entities; my $str = q("I have a 15" latter and a 6" foot arm."); my $re = qr/^("?)(.*?)("?)$/s; $str =~ s/$re/$1.encode_entities($2).$3/e; print $str, $/; __END__ "I have a 15" latter and a 6" foot arm."
The regex looks for an optional initial quote, arbitrary text, and an optional final quote, capturing all three (any may be empty). The substitution is exec'ed so that we can call the convenient encode_entities function. We might have used substr, but the presence of optional elements decided in favor of substitution.

Update: In CB, holli++ and Sidhekin++ pointed out to me that I'd overgeneralized the problem. Here is my take on the exact question asked, using substr,

use HTML::Entities; my $str = q("I have a 15" latter and a 6" foot arm."); substr($str, 1, -1) = encode_entities substr($str, 1, -1), q("); print $str, $/; __END__ "I have a 15" latter and a 6" foot arm."
That assumes the enclosing quotes are always present, and only encodes interior quotes. I also added the /s flag to the earlier substitution code so that newlines don't interfere.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: Another Regular Expression
by holli (Abbot) on Jan 09, 2005 at 08:16 UTC
    HTML::Entities is a good idea. here is a version that doesn´t need a regex:
    use HTML::Entities; my $str = q("I have a 15" latter and a 6" foot arm."); substr($str, 1, length($str)-2) = encode_entities(substr($str, 1, leng +th($str)-2)); print $str; #"I have a 15" latter and a 6" foot arm."