Solved

Regular Expression Help!

Posted on 2002-07-16
13
162 Views
Last Modified: 2010-03-05
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.
0
Comment
Question by:misterben
  • 3
  • 3
  • 3
  • +2
13 Comments
 
LVL 1

Expert Comment

by:smisk
ID: 7157860
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
 

Author Comment

by:misterben
ID: 7158351
cool

but what about...

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

Author Comment

by:misterben
ID: 7158387
cool

but what about...

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

Expert Comment

by:smisk
ID: 7158394
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
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 125 total points
ID: 7159056
$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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7159063
ops typo, please replace
  (["]*)
by
  ([^"]*)
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:misterben
ID: 7168462
thanks ahoffman...

won't your solution get upset with...

myapp.exe /a=1 /b="some /a=3 /c=2 text"
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7168957
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
 
LVL 39

Accepted Solution

by:
abel earned 125 total points
ID: 7259278
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
 
LVL 39

Expert Comment

by:abel
ID: 7259291
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
 
LVL 8

Expert Comment

by:davorg
ID: 9484061
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
 
LVL 39

Expert Comment

by:abel
ID: 9490884
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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now