I realized ... as soon as I saw it posted on the internet. I swear I have a better critical eye under that circumstance than I do my own screen sometimes.

A well known phenomenon. :-) Cf. Re: Move along, nothing to see here . . . and its two follow-ups, and Rubber_duck_debugging.

...can you explain these substitutions, in particular, the values chosen.

Nothing clever here. When I ran the code without substitutions, certain character entities failed to render correctly (on my Windows 8.1 system, in which the command window’s code page happens to be set to 1252). So I looked at the original as rendered in Google Chrome to determine what the entities were supposed to be, then wrote the substitutions accordingly. Of course, YMMV.

How is the action of s defined, and why does this one not have 3 matched symbols after it?

Substitution performs a regular expression match against the first half (LHS), and then, if a match is found, substitutes the second half (RHS) for whatever was matched. See “s/PATTERN/REPLACEMENT/msixpodualngcer” under perlop#Regexp-Quote-Like-Operators.

When choosing delimiters for a regular expression, you have a choice between bracketing (paired) and non-bracketing (unpaired) symbols. If you choose a non-bracketing delimiter — such as /, !, or # — you use that delimiter twice for a regex and three times for a substitution:

$x =~ m!\d+\s*\w+!; # Note that the "m" prefix is required except w +hen the delimiter is "/" $x =~ s!\d+\s*\w+!_!;

However, if you choose bracketing delimiters — (), [], {}, or <> — then a regex has one matching pair of delimiters, and a substitution has two pairs, or four symbols in total:

$x =~ m[\d+\s*\w+]; $x =~ s[\d+\s*\w+][_];

When delimiters are paired, they have to match each other; but in a substitution the second pair does not have to match the first pair. See perlop#Quote-and-Quote-like-Operators.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re^9: regex to get random quote by Athanasius
in thread regex to get random quote by Aldebaran

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.