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.
misterbenAsked:
Who is Participating?
 
abelCommented:
If you use VB to do the job, and you use it's regexp engine, I recommend you look up the Jenda.Rex engine instead, which gives you a COM-wrapper around Perl's RegExp engine. You can simply call it in VB as if it were a normal object and it's much easier to use then MS's own RegExp object.

That aside, you still need a regular expression. I think this works with both flavours (except for the non-grouping parentheses), but it assumes you use the "Command" statement of VB to get the commandline, meaning you don't have to parse the "myapp.exe" part.

Take the complete following line:
/([a-zA-Z])=((?:"[^"]+")|(?:[^ ])) +

$1 contains A,B,C..Z
$2 contains the part behind the equal sign (1  or "some /a=3 /c=2 text" or c:\dos\file.txt) including quotes if necessary.

Note that the order is important. If a quote comes, it is treated as a quoted string, if not, it reads up to the next space. Remove the ?: for use with VB, unless you use Jenda.Rex.

Regards,
Abel

0
 
smiskCommented:
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";
    }
}
0
 
misterbenAuthor Commented:
cool

but what about...

myapp.exe /a=1 /b="Some literal text with a / in it"
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
misterbenAuthor Commented:
cool

but what about...

myapp.exe /a=1 /b="Some literal text with a / in it"
0
 
smiskCommented:
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...
0
 
ahoffmannCommented:
$cmd = 'myapp.exe /a=1 /b="Some Text" /c=http://www.msn.com /d=c:\dos\file.txt';
quotemeta($cmd);
foreach $_ (split(" /",$cmd)){
  s#^.=["]*(["]*)["]*#$1#;
  print "$_\n";
}
# keep in mind that each / must be preceeded by a blank
0
 
ahoffmannCommented:
ops typo, please replace
  (["]*)
by
  ([^"]*)
0
 
misterbenAuthor Commented:
thanks ahoffman...

won't your solution get upset with...

myapp.exe /a=1 /b="some /a=3 /c=2 text"
0
 
ahoffmannCommented:
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.
0
 
abelCommented:
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])=(""[^""]+""|[^ ]) +"

0
 
davorgCommented:
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
0
 
abelCommented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.