>>$str1 = "^$a\\s+(b)(?:\\s+:\\s+$c\\s+$b)?\\s*";
This pattern will match the following
Code:
ax  bx     : cx     d
>>$str2 = "^$a\\s+(b)(?:\\s+:\\s+$c\\s+$b)?\\s*{";
So will this one, see the problem? :-) There's a left over x in the string that the regex doesn't handle, so the match fails. The reason the d is taken but not the x is here
Code:
$b = "(?:_)?\\w";
At the end you match a word character, this is the d :-) There're a bunch of ways to fix it as you'd expect with Perl, here's one
Code:
#!usr/bin/perl -w

$_ = "ax  bx     : cx     dx {";
$a = "ax";
$b = "(?:_)?";
$c = "cx";
$d = "dx";

$str1 = "^$a\\s+(bx)(?:\\s+:\\s+$c\\s+$b$d)?\\s*";
$str2 = "^$a\\s+(bx)(?:\\s+:\\s+$c\\s+$b$d)?\\s*{";

print "1." . m/$str1/ . "\n";
print "2." . m/$str2/ . "\n";