Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Why doesn't below work? (I am following perl rexp tutorial from perldoc.perl.org)
Using perl version 5.8.4
use warnings; use strict; my @dic = qw#beriberi hanabbs#; for (@dic) { print "trying to match $_\n"; print $_ if $_ =~ m#^(hana)(bbs)?(?(1)\2\1|\1)$#; }
yahoo@myserver ~/.yahoo/perl> !$ simple_grep.pl trying to match beriberi trying to match hanabbs yahoo@myserver ~/.yahoo/perl>

Replies are listed 'Best First'.
Re: conditional expression testing
by ikegami (Patriarch) on Jun 01, 2010 at 16:26 UTC
    Checks if the numbered capturing buffer has matched something.
    beri beri ^, then '(hana)' fails. Fail.
    hana bbs ^(hana) (bbs), then '\2' fails. Backtrack; ^(hana)\2, then '\1' fails. Fail.

    (?(1)...) doesn't make any sense since it's impossible to match the regex without the first capture matching something. Did you mean (?(2)...)?

    'beriberi' =~ m#^(beri)(bbs)?(?(2)\2\1|\1)$#;
    beri beri ^(beri)(bbs)?\1 $ Match.
    'beribbsbbsberi' =~ m#^(beri)(bbs)?(?(2)\2\1|\1)$#;
    beri bbs bbsberi ^(beri)(bbs)?\2 \1 $ Match.

    Update: Added working examples

      thank you guys. total misunderstanding on my part
      works perfectly now
      use warnings; use strict; my @dic = qw#beriberi hanabbsbbshana#; for (@dic) { my $pat = $_; print "trying to match $pat\n"; print "matched $pat\n" if $_ =~ m#^(hana)(bbs)?(?(1)\2\1|\1)$#; } my @pat = qw#AAAAG AC GC AZG ZC AAAG#; for ( @pat ) { my $pat2 = $_; print "matching $pat2\n"; print "matched $pat2\n" if $_ =~ m#[ATGC]+(?(?<=AA)G|C)$#; }
        m#^(hana)(bbs)?(?(1)\2\1|\1)$#;

        is the same as

        m#^(hana)(bbs)?\2\1$#;

        since (?(1)...) is always true. And since you don't use the captures, that can be be simplified to

        m#^hana(?:bbsbbs)?hana$#;
Re: conditional expression testing
by choroba (Cardinal) on Jun 01, 2010 at 16:24 UTC
    hanabbsbbshana matches your RE, though. Are you sure you wanted to use (?(1)? 1 is always true, so the no-pattern after | is never considered, so your RE is equivalent to /(hana)(bbs)?\2\1$/