in reply to regex extraction for variable number of args

The following gives the output you want with the provided example:

#!/usr/bin/env perl use 5.010; use strict; use warnings; my $cmd = '&COMPAREEQUAL(First-param.one, Second.param,Third-param)'; my $cms_re = qr{ (?: [&( ] )? ( [^(), ]+ ) (?: [(,)] )? }x; say for $cmd =~ m{$cms_re}g;

Output:

$ pm_cmd_args_regex.pl COMPAREEQUAL First-param.one Second.param Third-param

-- Ken

Replies are listed 'Best First'.
Re^2: regex extraction for variable number of args
by NetWallah (Canon) on Jul 21, 2012 at 14:13 UTC
    Thank you - this is beautiful, elegant, simple and operational.

    I modified it slightly to remove what I thought was unnecessary grouping - this still works:

    qr{ \&? # Leading char for Sub-name - ( [^(),\s]+ ) # Sub-name OR arg [(,)]? # Trailing "(" or comma }x;
    Thanks also to others who posted working solutions.

    As a learning experience, I would also like to understand why the repetition attempt in my O.P - the one without the /g - did not work. Any assistance on that ?

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

      A regular expression without /g will always match a string from the start, and only once. You need /g for making the engine try multiple times. Your for loop only gets one result. Whether that result is the first target or the last target hinges on where your regex can match.
        Ok - it is beginning to sink in - so - it appears that there is no useful/meaningful way to combine non-capturing parens and repetition, unless your pattern starts at the beginning - in which case, you are better-off with the "/g" modifier . Or you are always better-off with the "/g" modifier.

        So - is there a use case for non-capturing parens with repetition ?

                     I hope life isn't a big joke, because I don't get it.
                           -SNL