How to debug a regex with (?DEFINE)?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,818
Messages
2,569,736
Members
45,709
Latest member
DRISenaida

Latest Threads

Top