Solved

Regular Expression Help!

Posted on 2002-07-16
13
164 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
perl rename 2 142
Insert Text into odbc.ini file 15 64
.properties file to call function/method 9 59
Perl tar error 8 50
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
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…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 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