in reply to Many birds, single cage...

The first set of parens goes into $1, and the second (($rd) in this case) goes into $2. If you just want only one of the parenthesized bits, then you have to explicitly check $1, $2, and $3 yourself. Something like this:

$strange_thing =~ m/($large)|$wei($rd)|($stra)$nge/; my $piece = $1 || $2 || $3;

Hope this helps!

Replies are listed 'Best First'.
Re: Re: Many birds, single cage...
by Enlil (Parson) on Dec 09, 2003 at 21:41 UTC
    It would be better to see if the variable is defined rather than if it is true (via || )as 0 is not true and could very well be the pattern that matched.

    -enlil

Re: Re: Many birds, single cage...
by monsieur_champs (Curate) on Dec 09, 2003 at 21:28 UTC

    Dear duff
    Thank you, but I have near 50 capture parentesis on my real regular expression. It is a complete grammar for my company's application command-line parser. This was the first solution I tried and rejected, because it's unacceptable (and what if I need one more capture parentesis?).

    Thank you for the effort and for care about answering, anyway. This for sure will be usefull for another monk, in the future.


    "In few words, translating PerlMonks documentation and best articles to other languages is like building a bridge to join other Perl communities into PerlMonks family. This makes the family bigger, the knowledge greather, the parties better and the life easier." -- monsieur_champs

      If it's a complete grammar, then somethign like Parse::RecDescent or another parser generator may be a good bet for future development.

      You can do it all with regular expressions and captures, but I'm not sure why you would want to.

      If a proper parsing module is not an option, you could match one token at a time in a loop. From there you could build a recursive descent parser of your own or somesuch.



      Christopher E. Stith

        Fellow mr mischief
        I've never ever used Parse::RecDescent before. It worth the effort? The documentation is pretty big, seems that its a big and complex module. Except by some more regular expressions spread through the code, I'm basically using this as my "parser" (sorry about the portuguese comments, I can translate them on demand):

        I would love to read what the fellow monks think about this code, and if someone have any nice way to improve it. Thank you all for the answers and considerations, and may the gods bless you.


        "In few words, translating PerlMonks documentation and best articles to other languages is like building a bridge to join other Perl communities into PerlMonks family. This makes the family bigger, the knowledge greather, the parties better and the life easier." -- monsieur_champs