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" ); } } #### "123 123 " Match 1 => "123", Match 2 => " 123" "1 1 " No match "45 &45 " Match 1 => "45", Match 2 => "&45"