It would appear that the path to enlightenment is through practice, and not pedagoguery.

This has been my experience. "Regular expressions," as they have evolved (into ir-regular expressions, as others have noted), are the most counterintuitive things I have encountered in programming.

Here's my favorite example of this conceptual orneriness. What will the regex  /(b*)/ capture when run against the string  'aaaaabbbb' and where (i.e., at what character position offset)? Knowing as we do that  * matches "as much as possible" of something, I'd almost be willing to bet money that even the most regex-savvy will experience at least a minor knee-jerk twitch in the general direction of "it matches  'bbbb' at offset 5." But surprise, surprise:

c:\@Work\Perl\monks>perl -wMstrict -le "my $s = 'aaaaabbbb'; print qq{matches '$1' at offset $-[1]} if $s =~ /(b*)/; " matches '' at offset 0
It matches our old friend the empty string at a location as far away from any  'b' as it could possibly get.

The answer to the puzzle is that "as much as possible" cheats and leaves out the leftmost and equally important part of the "leftmost longest" incantation that should properly be used* to describe all regex matching. Bottom line: No amount of theoretic or pedagogic vaccination can build up your natural antibody resistance to this sort of thought-bug better than daily exposure to a wide variety of regex challenges. Good luck in your experimentation, and may you make many mistakes, for I know no better way to learn this stuff.

* Old Whateley and his son Wilbur knew the dangers of incomplete and possibly maliciously altered incantations.


Give a man a fish:  <%-{-{-{-<


In reply to Re^3: Simplifying regexes by AnomalousMonk
in thread Simplifying regexes by ExReg

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.