$a="a "; foreach (1..(length $a)) { print "a(".$1.")(".$2.")\n" if ( $a =~ m/^a( {$_})( )/ ) ; }