I recommend the HTML::Entities module for that sort of job.
That doesn't cover omitting leading and trailing quotes, so lets cook up a regex and a substitution,$ 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.$
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.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."
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,
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.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."
After Compline,
Zaxo
In reply to Re: Another Regular Expression
by Zaxo
in thread Another Regular Expression
by akm2
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |