Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Regex to match command line arguments

by Anonymous Monk
on May 08, 2003 at 12:38 UTC ( #256511=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Writing some code that builds the VM args string for a java program. I'm completely regex ignorant, was wondering if someone could help...

Given a string containing the following:

-Dname1=2 -Dname2.3="anything" -Djetty.home=c:\a\b\c or -Dname1=2 -Dname2.3="anything" -jetty.home="c:\a\b\c"

What would be a pattern that matches:

-Djetty.home=anything_up_to-whitespace_or_eol or -Djetty.home="anything_up_to[" or eol]

make sense? Thanks! Geoff

update (broquaint): title change (was Regex match newbie)

Comment on Regex to match command line arguments
Select or Download Code
Re: Regex to match command line arguments
by Skeeve (Vicar) on May 08, 2003 at 12:48 UTC
    so you want to get the parameter given on the command line?
    /-Djetty.home=("[^"]*"|\S*)/
    This should work but doesn't take quotes in quotes into account.
      Perfect! Thank you wery much!
Re: Regex to match command line arguments
by broquaint (Abbot) on May 08, 2003 at 12:50 UTC
    What would be a pattern that matches:
    Something like this should do the trick
    my $re = qr/\s -D?jetty\.home= (.+?) (?:\s|$) /x; /$re/ and print "yup - $1" for q[-Dname1=2 -Dname2.3="anything" -jetty.home="c:\a\b\c"], q[-Dname1=2 -Dname2.3="anything" -Djetty.home=c:\a\b\c] __output__ yup - "c:\a\b\c" yup - c:\a\b\c
    See. perlre for more info.
    HTH

    _________
    broquaint

Re: Regex to match command line arguments
by jdporter (Canon) on May 08, 2003 at 13:32 UTC
    If you're just trying to extract the value of the parameter -Djetty.home, then this will work:
    /-Djetty\.home=(?:(:?"(.*?)")|(\S*))/;
    but with the caveat that the result (the value of the param) is in $+, not in $1.

    jdporter
    The 6th Rule of Perl Club is -- There is no Rule #6.

Re: Regex to match command line arguments
by pzbagel (Chaplain) on May 08, 2003 at 14:54 UTC

    I'm not sure your second example data makes sense. But then, I am not familiar with java VM. On a side note, if you are dealing with a number of arguments, a more general solution would be:

    #If they arguments are in @ARGV while (shift){ ($arg,$val)=split /=/; $args{$arg}=$val; }

    Now you will find all your command line arguments in the hash %args with the argument name as the key, like this:

    '-Dname1' => 2 '-Dname2.3' => "anything" '-Djetty.home' => 'c:\a\b\c'

    Hope that's what you are looking for.

Re: Regex to match command line arguments
by LordWeber (Monk) on May 08, 2003 at 16:21 UTC
    Maybe you also find a module which makes it a bit easier to handle the arguments in general. Maybe one of the Opt:: Modules is what you're seeking for. Just have a look at CPAN.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://256511]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2014-10-24 16:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (133 votes), past polls