Here is one way to build a character class based on a backreference. Note, I've had to use the (??{....}) construct, and I'm not positive (without diving again into the gory details of parsing) whether I'm relying on defined behavior or just happenstance. But it works!

use strict; use warnings; while ( my $string = <DATA> ) { chomp $string; if ( $string =~ m/(\w+)\s((??{"[$1]+"}))/ ) { print "$string => matched: $1, $2!\n"; } else { print "$string => Didn't match.\n"; } } __DATA__ abcde fgh abcde eadcabe

With that snippet, the first line will fail to match, and the second line will succeed, because the second half of the second line contains only those characters found in the first subset. This could probably be accomplished with greater simplicity by just breaking it down into smaller regexps that cascade from one to the next, but I couldn't resist the challenge of doing it in one.

Update: Having just re-read perlre, I'm satisfied that I'm relying on defined (though "experimental") behavior. The (??{...}) subexpression is a sort of postponed regular subexpression, and it should have full access to the $n ($1, $2, etc.) special variables for any parens that have actuall matched so far. I could also have written the (??{...}) subexpression as

(??{"[$^N]+"})
...because $^N is the same as the $1, $2, etc. variables but contains the most recent successful capturing subexpression.


Dave


In reply to Re: \1, \2, \3, ... inside of a character class by davido
in thread \1, \2, \3, ... inside of a character class by ccn

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.