BTW: Here's a version that handles any number of capture groups.
Question: Why does capture group 2 ($2) in the  (\w+).*(\d{2,}).*(\1) example only capture '95'? Shouldn't  (\d{2,}) match and capture "the maximum of 2 or more decimal digits", i.e., '495', as it did in the third example?

>perl -wMstrict -le "$_ = '1:A silly sentence (495,a), silly but useful.(3)'; ;; EXPRESSION: { print qq{\n}; print 'Enter a regular expression:'; my $expression = <STDIN>; last EXPRESSION unless $expression =~ m{ \S }xms; chomp($expression); print qq{Expression is '$expression'}; ;; if (! defined($expression = eval qq{qr/$expression/})) { print qq{Regex error: $@}; redo EXPRESSION; } ;; if ($_ !~ $expression) { print 'The expression does not match the string'; redo EXPRESSION; } print 'The expression matches the string'; ;; if ($#- < 1) { print qq{No capture groups}; redo EXPRESSION; } ;; for my $cg (1 .. $#-) { printf qq{capture group \$$cg is '%s' starting at offset %d \n}, substr($_, $-[$cg], $+[$cg]-$-[$cg]), $-[$cg]; } redo EXPRESSION; } ;; print 'done'; " Enter a regular expression: foo Expression is 'foo' The expression does not match the string Enter a regular expression: \d{2,} Expression is '\d{2,}' The expression matches the string No capture groups Enter a regular expression: (\d{2,}) Expression is '(\d{2,})' The expression matches the string capture group $1 is '495' starting at offset 20 Enter a regular expression: (\w+).*(\1) Expression is '(\w+).*(\1)' The expression matches the string capture group $1 is 'silly' starting at offset 4 capture group $2 is 'silly' starting at offset 28 Enter a regular expression: (\w+).*(\d{2,}).*(\1) Expression is '(\w+).*(\d{2,}).*(\1)' The expression matches the string capture group $1 is 'silly' starting at offset 4 capture group $2 is '95' starting at offset 21 capture group $3 is 'silly' starting at offset 28 Enter a regular expression: \d*** Expression is '\d***' Regex error: Nested quantifiers in regex; marked by <-- HERE in m/\d** <-- HERE */ at (eval 6) line 1, <STDIN> line 6. Enter a regular expression: silly Expression is 'silly' The expression matches the string No capture groups Enter a regular expression: done

In reply to Re^3: Regex backreference by AnomalousMonk
in thread Regex backreference by vyeddula

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.