http://www.perlmonks.org?node_id=256511

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)

Replies are listed 'Best First'.
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 Skeeve (Parson) 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 jdporter (Paladin) 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