How to debug a regex with (?DEFINE)?

Discussion in 'Perl Misc' started by Peng Yu, Aug 7, 2012.

  1. Peng Yu

    Peng Yu Guest

    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
    Peng Yu, Aug 7, 2012
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. theotyflos
    Replies:
    3
    Views:
    468
    Thomas Matthews
    Feb 19, 2004
  2. robin liu
    Replies:
    3
    Views:
    821
    Robin Liu
    Apr 21, 2006
  3. FBM

    #iidef and #define to debug program

    FBM, Apr 30, 2006, in forum: C Programming
    Replies:
    2
    Views:
    294
    Peter Shaggy Haywood
    May 2, 2006
  4. Replies:
    3
    Views:
    757
    Reedick, Andrew
    Jul 1, 2008
  5. Brian Takita

    #define _ and #define __

    Brian Takita, Jan 23, 2006, in forum: Ruby
    Replies:
    0
    Views:
    460
    Brian Takita
    Jan 23, 2006
Loading...

Share This Page