Peng said:
Hi,
If I used the uncommented if-statement, I would get no match. If I
used the commend if statement otherwise, I would have the following
string as the output. I'm wondering why the regular expression with *
does not match anything?
It does match, it just doesn't match what you expected it to match.
namespace a { namespace b { namespace c {
Thanks,
Peng
$string="a namespace a { namespace b { namespace c { ";
#if ($string =~ /\s*((namespace\s+\w(\w|\d)*\s*\{\s*)+)/) {
if ($string =~ /\s*((namespace\s+\w(\w|\d)*\s*\{\s*)*)/) {
print "$1\$\n";
}
$ perl -e'
use re qw/ debug /;
my $string = "a namespace a { namespace b { namespace c { ";
if ($string =~ /\s*((namespace\s+\w(\w|\d)*\s*\{\s*)*)/) {
print "$1\$\n";
}
'
Compiling REx `\s*((namespace\s+\w(\w|\d)*\s*\{\s*)*)'
size 40 Got 324 bytes for offset annotations.
first at 1
1: STAR(3)
2: SPACE(0)
3: OPEN1(5)
5: CURLYX[0] {0,32767}(37)
7: OPEN2(9)
9: EXACT <namespace>(13)
13: PLUS(15)
14: SPACE(0)
15: ALNUM(16)
16: CURLYM[3] {0,32767}(28)
20: BRANCH(22)
21: ALNUM(26)
22: BRANCH(24)
23: DIGIT(26)
26: SUCCEED(0)
27: NOTHING(28)
28: STAR(30)
29: SPACE(0)
30: EXACT <{>(32)
32: STAR(34)
33: SPACE(0)
34: CLOSE2(36)
36: WHILEM[1/2](0)
37: NOTHING(38)
38: CLOSE1(40)
40: END(0)
minlen 0
Offsets: [40]
3[1] 1[2] 4[1] 0[0] 37[1] 0[0] 5[1] 0[0] 6[9] 0[0] 0[0] 0[0]
17[1] 15[2] 18[2] 27[1] 0[0] 20[1] 0[0] 20[1] 21[2] 23[1] 24[2] 26[1]
0[0] 27[0] 27[0] 30[1] 28[2] 31[2] 0[0] 35[1] 33[2] 36[1] 0[0] 37[0]
37[0] 38[1] 0[0] 39[0]
Matching REx "\s*((namespace\s+\w(\w|\d)*\s*\{\s*)*)" against "a
namespace a { namespace b { namespace c { "
Setting an EVAL scope, savestack=5
0 <> <a namespace > | 1: STAR
SPACE can match 0 times out of 2147483647...
Setting an EVAL scope, savestack=5
0 <> <a namespace > | 3: OPEN1
0 <> <a namespace > | 5: CURLYX[0] {0,32767}
0 <> <a namespace > | 36: WHILEM[1/2]
0 out of 0..32767 cc=bfa0d330
Setting an EVAL scope, savestack=15
0 <> <a namespace > | 7: OPEN2
0 <> <a namespace > | 9: EXACT <namespace>
failed...
restoring \1 to -1(0)..-1(no)
restoring \1..\3 to undef
failed, try continuation...
0 <> <a namespace > | 37: NOTHING
0 <> <a namespace > | 38: CLOSE1
0 <> <a namespace > | 40: END
Match successful!
$
Freeing REx: `"\\s*((namespace\\s+\\w(\\w|\\d)*\\s*\\{\\s*)*)"'
You see where it says "Match successful!", that means that the
expression (namespace\s+\w(\w|\d)*\s*\{\s*)* matched zero times.
Also, the expression \w(\w|\d)* could be simplified to \w+.
John