It was a great revelation when I first learned of the 'e' flag for regexes (when I learned of the double 'ee' it was more fear inspiring than revelatory). This is a simple play at a kilometer/mile converter. I'd love to see others' samples here of regexes that execute code.
my $sentence = ( join " ", @ARGV ) || "Give me a sentence containing kilometers or miles or both.\n" . qq| Eg (note quotes): "I live 6 miles from here."|; $sentence =~ s! # we're substituting \b(\d+(?:\.\d+)?)\b # find a number \s+ # followed by space (kilometer|mile)s? # "km"s or "m"s ! # 1/2 done sprintf "%.1f %s%s", # convert, $1 * ($2eq'mile'?1.6:.63), # if "m"s, use 1.6 ($2eq'mile'?'kilometer':'mile'), # if "km"s, use .63 ($1*($2eq'mile'?1.6:.63))==1?'':'s' # do the math !xieg; # ignore, execute, repeat print $sentence, "\n";

In reply to mile/kilometer converter (category: stupid regex tricks) by Your Mother

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.