Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: regex extraction for variable number of args

by kcott (Archbishop)
on Jul 21, 2012 at 07:14 UTC ( [id://982957]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://982957]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-24 05:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found