Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to debug a regex with (?DEFINE)?

Reply
Thread Tools

How to debug a regex with (?DEFINE)?

 
 
Peng Yu
Guest
Posts: n/a
 
      08-07-2012
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
Config Mgr Debug/Release and Web.config Compilation debug=true RonL ASP .Net 0 04-08-2006 03:50 PM
Debug (DLL MFC) -> Debug (Static MFC) ringos75 C++ 0 04-14-2005 01:50 PM
[Howto] Compiling debug Python extensions for non-debug Python Mike C. Fletcher Python 3 10-12-2003 09:37 PM



Advertisments