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" ); } }