Link to home
Start Free TrialLog in
Avatar of misterben
misterben

asked on

Regular Expression Help!

I need a regular expression to parse a command line,
where the command line can look like..

myapp.exe /a=1 /b="Some Text" /c=http://www.msn.com /d=c:\dos\file.txt

I need the values..

1
Some Text
http://www.msn.com
c:\dos\file.txt

Bonus points if u can show example using vb regexp object.
Avatar of smisk
smisk

Can't help you with the VB thing, but hows this for getting the perl stuff?

$cmd = 'myapp.exe /a=1 /b="Some Text" /c=http://www.msn.com /d=c:\dos\file.txt';

%args = split(" /", $cmd);
foreach $item (%args) {
    if($item =~ /=/) {
        $val = (split("=", $item))[1];
        $val =~ s/"//g;
        print "$val\n";
    }
}
Avatar of misterben

ASKER

cool

but what about...

myapp.exe /a=1 /b="Some literal text with a / in it"
cool

but what about...

myapp.exe /a=1 /b="Some literal text with a / in it"
Well, if you are running "myapp.exe" as the actual program try :

#! /usr/bin/perl

foreach (@ARGV) {
    s#/.*?=##;
    print;
    print "\n";
}

... but keep in mind that the shell interprets "\" as an escape character, so "C:\dos\file.txt" will come out as "C:dosfile.txt" unless you put it in quotes...

If "myapp.exe" is a part of the string to be parsed instead of the actual program just do a "if(/=/) { " around the substitutions and prints...
SOLUTION
Avatar of ahoffmann
ahoffmann
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ops typo, please replace
  (["]*)
by
  ([^"]*)
thanks ahoffman...

won't your solution get upset with...

myapp.exe /a=1 /b="some /a=3 /c=2 text"
yes, my suggestion does not what you probably expect :-o
In this case you need to write a more sophisticated parser, but that requires a full list of recommendations for that.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Typos come in easily, here's one:
(?:[^ ])
should read:
(?:[^ ]+)

By the way, I don't think the non-capturing (I said non-grouping, but I meant non-capturing) parentheses are really necessary. You can just leave them out:

/([a-zA-Z])=("[^"]+"|[^ ]) +

Btw2: Jenda.Rex is here: http://jenda.krynicky.cz/#Jenda.Rex

Btw3: depending on what system you use it, you may need some escaping. For VB it looks like this:

Dim sRegExp As String
sRegExp = "/([a-zA-Z])=(""[^""]+""|[^ ]) +"

Avatar of Dave Cross
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

PAQ/No Refund

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

davorg
EE Cleanup Volunteer
Well, my first and second comment do give a working solution for misterben, though it's a pity he never answered. I don't really care about the points, but it would be great if my (or ahoffman's) comment will be treated as an answer.

greetz,
Abel