in reply to Backref in a regex quantifier

The following works (note the lack of curly braces in the back-reference):
use strict; foreach ( "123 123 ", "1 1 ", "45 &45 " ) { print( "\"$_\"\n " ); if ( /^(\d+?) (.\1) / ) { printf( "Match 1 => \"%s\", Match 2 => \"%s\"\n", $1, $2 ); } else { print( "No match\n" ); } }

Outputs:

"123 123 " Match 1 => "123", Match 2 => " 123" "1 1 " No match "45 &45 " Match 1 => "45", Match 2 => "&45"

Update: corrected spelling in sentence.

Replies are listed 'Best First'.
Re^2: Backref in a regex quantifier
by Crackers2 (Parson) on Jul 09, 2007 at 13:57 UTC

    That doesn't do what the OP wants though. You're just matching the back reference again.

    If I understand it correctly, what the OP wants is to have a match on the string "3 56789" return 3 in $1 and 567 in $2