P
Peng Yu
Hi,
I'm trying to extract the nested namespace in the following code. But
the code can only extract the inner namespace. It is very hard for me
to see what is wrong. Does anybody know some tricks how to debug a
regex like the following? Thanks!
~/linux/test/perl/man/perlre/(?/(/DEFINE$ cat
main_namespace_multiple.pl
#!/usr/bin/env perl
use strict;
use warnings;
my $text=<<'EOF';
namespace A {
namespace B {
}
}
EOF
# Build pattern that matches only namespaces...
my $namespace_pattern = qr{
((?&namespace)) # Match and capture (possibly nested)
namespace
# Define each component...
(?(DEFINE)
(?<namespace_token>
\b [A-Za-z_]\w* \b
)
(?<namespace_keyword>
\b namespace \b
)
# Namespace is keyword + name + block...
(?<namespace>
(?&namespace_keyword) \s+ (?&namespace_token) \s*
\{
(?&namespace_body)
\}
)
(?<namespace_body>
(?:
\s*
(?&namespace)
\s*
)
|
(?&block)
)
(?<block>
\{
(?: (?&block) | . )*?
\}
)
)
}xs;
my ($extracted) = $text =~ $namespace_pattern;
print "text = $text\n";
print "extracted = $extracted\n";
Regards,
Peng
I'm trying to extract the nested namespace in the following code. But
the code can only extract the inner namespace. It is very hard for me
to see what is wrong. Does anybody know some tricks how to debug a
regex like the following? Thanks!
~/linux/test/perl/man/perlre/(?/(/DEFINE$ cat
main_namespace_multiple.pl
#!/usr/bin/env perl
use strict;
use warnings;
my $text=<<'EOF';
namespace A {
namespace B {
}
}
EOF
# Build pattern that matches only namespaces...
my $namespace_pattern = qr{
((?&namespace)) # Match and capture (possibly nested)
namespace
# Define each component...
(?(DEFINE)
(?<namespace_token>
\b [A-Za-z_]\w* \b
)
(?<namespace_keyword>
\b namespace \b
)
# Namespace is keyword + name + block...
(?<namespace>
(?&namespace_keyword) \s+ (?&namespace_token) \s*
\{
(?&namespace_body)
\}
)
(?<namespace_body>
(?:
\s*
(?&namespace)
\s*
)
|
(?&block)
)
(?<block>
\{
(?: (?&block) | . )*?
\}
)
)
}xs;
my ($extracted) = $text =~ $namespace_pattern;
print "text = $text\n";
print "extracted = $extracted\n";
Regards,
Peng