Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Regular Expression Help!

Posted on 2002-07-16
13
Medium Priority
?
171 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 375 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
 

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 375 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

610 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