in reply to Calculating variable {n} in regex match

I learned this here at PerlMonks: you can execute an expression inside a string or a regex pattern by wrapping it in @{[]}, like so:

#!/usr/bin/env perl use Modern::Perl; my $str = 'abc123efghijk'; my $i = 2; if( $str =~ /(\d{@{[$i+1]}})/ ){ say $1; } # output -> 123

Aaron B.
Available for small or large Perl jobs; see my home node.

Replies are listed 'Best First'.
Re^2: Calculating variable {n} in regex match
by atreyu (Sexton) on Jun 18, 2012 at 16:59 UTC
    nice, aaron.

    my $rc = ($foo =~ /^(\d{@{[$i+1]}})$/

    always w/Perl, TIMTOWTDI...