First (.) matches "a" and stores it in $1

Second (.) matches "b" and stores it in $2

\2 matches the value of $2 , it matches "b"

\1 matches the value of $1 , it matches "a"

use re 'debug';

$ perl -Mre=debug -le " q/yabba dabba doo/ =~ /y(.)(.)\2\1/ " Compiling REx "y(.)(.)\2\1" Final program: 1: EXACT <y> (3) 3: OPEN1 (5) 5: REG_ANY (6) 6: CLOSE1 (8) 8: OPEN2 (10) 10: REG_ANY (11) 11: CLOSE2 (13) 13: REF2 (15) 15: REF1 (17) 17: END (0) anchored "y" at 0 (checking anchored) minlen 3 Guessing start of match in sv for REx "y(.)(.)\2\1" against "yabba dab +ba doo" Found anchored substr "y" at offset 0... Guessed: match at offset 0 Matching REx "y(.)(.)\2\1" against "yabba dabba doo" 0 <> <yabba dabb> | 1:EXACT <y>(3) 1 <y> <abba dabba> | 3:OPEN1(5) 1 <y> <abba dabba> | 5:REG_ANY(6) 2 <ya> <bba dabba > | 6:CLOSE1(8) 2 <ya> <bba dabba > | 8:OPEN2(10) 2 <ya> <bba dabba > | 10:REG_ANY(11) 3 <yab> <ba dabba d> | 11:CLOSE2(13) 3 <yab> <ba dabba d> | 13:REF2(15) 4 <yabb> <a dabba do> | 15:REF1(17) 5 <yabba> < dabba doo> | 17:END(0) Match successful! Freeing REx: "y(.)(.)\2\1"

YAPE::Regex::Explain

use YAPE::Regex::Explain; print YAPE::Regex::Explain->new( qr/y(.)(.)\2\1/ )->explain; __END__ The regular expression: (?-imsx:y(.)(.)\2\1) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- y 'y' ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- . any character except \n ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- ( group and capture to \2: ---------------------------------------------------------------------- . any character except \n ---------------------------------------------------------------------- ) end of \2 ---------------------------------------------------------------------- \2 what was matched by capture \2 ---------------------------------------------------------------------- \1 what was matched by capture \1 ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------

In reply to Re: Regex from Learning Perl by Anonymous Monk
in thread Regex from Learning Perl by Anonymous Monk

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.