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


In reply to Re: Another Regular Expression by Zaxo
in thread Another Regular Expression by akm2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.