473,569 Members | 3,040 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regex help?

I'm looking for help with a regular expression question, so my first
question is which newsgroup is the best one to post to? Just in case *this*
is the best choice, here's the problem:

I'm trying to "parse" something that looks like a command line; e.g.,

op arg1, arg2, ..., argn

The individual parts (op, arg1, ...) can be matched with a \w+ pattern --
except that the args *might* be quoted to cover the case where they contain
non \w characters. I've figured out what I think is the "right" pattern, but
what I'm unsure of is how to interpret the results.

What I'm trying to accomplish is to perform a Match operation, then walk
through the resulting match groups and their respective captures, producing
an array containing the arguments. The problem I have is determining the
*order* of things in the captures. Here's the detail...

The pattern for matching the args is an alternation: ((\w+)|"([^"]*)"). This
pattern of course occurs within a repeating outer group so for *this* group
there will be a capture collection with multiple elements. The problem of
course is that the captures will include the quote chars for those cases
where the right alternative was matched. What I want is just what's inside
the quotes. The inner groups (\w+) and ([^"]*) of course have their own
capture collections and these contain exactly what I want, but I don't see
how to determine the order.

To see the problem consider the case: op a, "b", c. The capture group for
the group containing the alternation will contain a, "b", c while the
capture group for the left subgroup will contain a, c and the right subgroup
will contain b. How can I tell the relative order so I can "merge" these
into a, b, c which is the desired result?

Of course I can just use the outer group and reparse the elements of the
capture collection, stripping out the quote chars where necessary, but I'd
like to know how this could be done just using regular expressions. Is it
possible?

Thanks in advance.

Bill
Jul 21 '05 #1
5 1538
Bill,

I hate Regex, therefore here two links

RegexLib
http://www.regexlib.com/Default.aspx

Expresso
http://www.ultrapico.com/Expresso.htm

I hope this helps a little bit?

Cor
Jul 21 '05 #2
Cor
Thanks for the response. These look like pretty good sites for finding
patterns, but unfortunately that's not my problem -- at least I don't think
so. What I need is some help in interpreting the results and in a .Net
specific way since I believe that capture collections are unique to .Net
regex.

Regards,
Bill

"Cor Ligthert" <no************ @planet.nl> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Bill,

I hate Regex, therefore here two links

RegexLib
http://www.regexlib.com/Default.aspx

Expresso
http://www.ultrapico.com/Expresso.htm

I hope this helps a little bit?

Cor

Jul 21 '05 #3
"Bill Cohagan" <co*****@teraXN OSPAMXquest.com > wrote in
news:eT******** *****@TK2MSFTNG P12.phx.gbl...
I'm looking for help with a regular expression question, so my first
question is which newsgroup is the best one to post to? Just in case
*this*
is the best choice, here's the problem:

I'm trying to "parse" something that looks like a command line; e.g.,

op arg1, arg2, ..., argn

The individual parts (op, arg1, ...) can be matched with a \w+ pattern --
except that the args *might* be quoted to cover the case where they
contain
non \w characters. I've figured out what I think is the "right" pattern,
but
what I'm unsure of is how to interpret the results.

What I'm trying to accomplish is to perform a Match operation, then walk
through the resulting match groups and their respective captures,
producing
an array containing the arguments. The problem I have is determining the
*order* of things in the captures. Here's the detail...

The pattern for matching the args is an alternation: ((\w+)|"([^"]*)").
This
pattern of course occurs within a repeating outer group so for *this*
group
there will be a capture collection with multiple elements. The problem of
course is that the captures will include the quote chars for those cases
where the right alternative was matched. What I want is just what's inside
the quotes. The inner groups (\w+) and ([^"]*) of course have their own
capture collections and these contain exactly what I want, but I don't see
how to determine the order.

To see the problem consider the case: op a, "b", c. The capture group for
the group containing the alternation will contain a, "b", c while the
capture group for the left subgroup will contain a, c and the right
subgroup
will contain b. How can I tell the relative order so I can "merge" these
into a, b, c which is the desired result?

Of course I can just use the outer group and reparse the elements of the
capture collection, stripping out the quote chars where necessary, but I'd
like to know how this could be done just using regular expressions. Is it
possible?


You could use the "Index" property of the capture objects to get the
captures into order, but I'm not sure if that's the kind of "elegant"
solution you're looking for.

Can't you use the "Matches" method, and let it return one Match item per
"parameter" ? That would certainly make things easier. I guess the problem
here is the "op" part; Maybe you could strip that away first: Use one regex
that splits your string into "operation" and "parameter list", and a second
one to split the parameters. (I think that's the "standard regex way", as
many regex engines don't track capture objects)

Hope this helps,

Niki
Jul 21 '05 #4
Niki
I hadn't thought of looking at the Index property -- that might be what I
need. As for the Matches method I don't think it'd solve the problem since I
have groups whose matches would be included in the collection, but whose
values I don't want to see. (Actually it might be possible to use "non
capturing" groups in conjunction with Matches to do something ...)

Thanks for the suggestions. I'll post back with what I find.

Bill

"Niki Estner" <ni*********@cu be.net> wrote in message
news:OQ******** ******@TK2MSFTN GP15.phx.gbl...
"Bill Cohagan" <co*****@teraXN OSPAMXquest.com > wrote in
news:eT******** *****@TK2MSFTNG P12.phx.gbl...
I'm looking for help with a regular expression question, so my first
question is which newsgroup is the best one to post to? Just in case
*this*
is the best choice, here's the problem:

I'm trying to "parse" something that looks like a command line; e.g.,

op arg1, arg2, ..., argn

The individual parts (op, arg1, ...) can be matched with a \w+ pattern -- except that the args *might* be quoted to cover the case where they
contain
non \w characters. I've figured out what I think is the "right" pattern,
but
what I'm unsure of is how to interpret the results.

What I'm trying to accomplish is to perform a Match operation, then walk
through the resulting match groups and their respective captures,
producing
an array containing the arguments. The problem I have is determining the
*order* of things in the captures. Here's the detail...

The pattern for matching the args is an alternation: ((\w+)|"([^"]*)").
This
pattern of course occurs within a repeating outer group so for *this*
group
there will be a capture collection with multiple elements. The problem of course is that the captures will include the quote chars for those cases
where the right alternative was matched. What I want is just what's inside the quotes. The inner groups (\w+) and ([^"]*) of course have their own
capture collections and these contain exactly what I want, but I don't see how to determine the order.

To see the problem consider the case: op a, "b", c. The capture group for the group containing the alternation will contain a, "b", c while the
capture group for the left subgroup will contain a, c and the right
subgroup
will contain b. How can I tell the relative order so I can "merge" these
into a, b, c which is the desired result?

Of course I can just use the outer group and reparse the elements of the
capture collection, stripping out the quote chars where necessary, but I'd like to know how this could be done just using regular expressions. Is it possible?
You could use the "Index" property of the capture objects to get the
captures into order, but I'm not sure if that's the kind of "elegant"
solution you're looking for.

Can't you use the "Matches" method, and let it return one Match item per
"parameter" ? That would certainly make things easier. I guess the problem
here is the "op" part; Maybe you could strip that away first: Use one

regex that splits your string into "operation" and "parameter list", and a second one to split the parameters. (I think that's the "standard regex way", as
many regex engines don't track capture objects)

Hope this helps,

Niki

Jul 21 '05 #5
Niki

Thanks again for the suggestions. I was able to accomplish the goal using
the Index property as the key in a merge of the two capture groups. Works
like a charm.

I'd hoped to do this all with regular expressions, but at least the code I
had to write was fun. I finally got to use a Queue!

Bill

"Niki Estner" <ni*********@cu be.net> wrote in message
news:OQ******** ******@TK2MSFTN GP15.phx.gbl...
"Bill Cohagan" <co*****@teraXN OSPAMXquest.com > wrote in
news:eT******** *****@TK2MSFTNG P12.phx.gbl...
I'm looking for help with a regular expression question, so my first
question is which newsgroup is the best one to post to? Just in case
*this*
is the best choice, here's the problem:

I'm trying to "parse" something that looks like a command line; e.g.,

op arg1, arg2, ..., argn

The individual parts (op, arg1, ...) can be matched with a \w+ pattern -- except that the args *might* be quoted to cover the case where they
contain
non \w characters. I've figured out what I think is the "right" pattern,
but
what I'm unsure of is how to interpret the results.

What I'm trying to accomplish is to perform a Match operation, then walk
through the resulting match groups and their respective captures,
producing
an array containing the arguments. The problem I have is determining the
*order* of things in the captures. Here's the detail...

The pattern for matching the args is an alternation: ((\w+)|"([^"]*)").
This
pattern of course occurs within a repeating outer group so for *this*
group
there will be a capture collection with multiple elements. The problem of course is that the captures will include the quote chars for those cases
where the right alternative was matched. What I want is just what's inside the quotes. The inner groups (\w+) and ([^"]*) of course have their own
capture collections and these contain exactly what I want, but I don't see how to determine the order.

To see the problem consider the case: op a, "b", c. The capture group for the group containing the alternation will contain a, "b", c while the
capture group for the left subgroup will contain a, c and the right
subgroup
will contain b. How can I tell the relative order so I can "merge" these
into a, b, c which is the desired result?

Of course I can just use the outer group and reparse the elements of the
capture collection, stripping out the quote chars where necessary, but I'd like to know how this could be done just using regular expressions. Is it possible?
You could use the "Index" property of the capture objects to get the
captures into order, but I'm not sure if that's the kind of "elegant"
solution you're looking for.

Can't you use the "Matches" method, and let it return one Match item per
"parameter" ? That would certainly make things easier. I guess the problem
here is the "op" part; Maybe you could strip that away first: Use one

regex that splits your string into "operation" and "parameter list", and a second one to split the parameters. (I think that's the "standard regex way", as
many regex engines don't track capture objects)

Hope this helps,

Niki

Jul 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4782
by: Dave | last post by:
I'm struggling with something that should be fairly simple. I just don't know the regext syntax very well, unfortunately. I'd like to parse words out of what is basically a boolean search string. It's actually the input string into a Microsoft Index Server search. The string will consist of words, perhaps enclosed in quotes or...
20
8061
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex matches are called within a loop (like if or for). E.g. for(int i = 0; i < 10; i++) { Regex r = new Regex();
17
3949
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher http://forta.com/books/0672325667/
7
2219
by: Mike Labosh | last post by:
I have the following System.Text.RegularExpressions.Regex that is supposed to remove this predefined list of garbage characters from contact names that come in on import files : Dim _dropContactGarbage As New Regex( _ "(+)|" & _ "(+)|" & _ "(+)|" & _ "(+)|" & _ "(+)|" & _
9
2074
by: jmchadha | last post by:
I have got the following html: "something in html ... etc.. city1... etc... <a class="font1" href="city1.html" onclick="etc."click for <b>info</bon city1 </a> ... some html. city1.. can repeat lot of times here.... Requirement: ------------------- I want to get the value of "href" i.e "city1.html" by searching "city1" between the...
4
2626
by: Chris | last post by:
Hi Everyone, I am using a regex to check for a string. When all the file contains is my test string the regex returns a match, but when I embed the test string in the middle of a text file a match is never returned. The string that I give to the regex is one that contains the entire contents of a text file. I'm using the multi-line...
7
2572
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward slash then some numbers. For some reason I am not getting that. It won't work at all in 2.0
6
4197
by: Phil Barber | last post by:
I am using Regex to validate a file name. I have everything I need except I would like the dot(.) in the filename only to appear once. My question is it possible to allow one instance of character but not two or more? example myfile.doc = good My.file.doc = not good if you could give an example of the expression pattern that would most...
1
12158
by: jonnyboy6969 | last post by:
Hi All Really hoping someone can help me out here with my deficient regex skills :) I have a function which takes a string of HTML and replaces a term (word or phrase) with a link. The pupose is that I seek out terms which are in a glossary on our site, and automatically link to this definition. Its slightly complex becase certain elements...
0
1617
by: Support Desk | last post by:
That’s it exactly..thx -----Original Message----- From: Reedick, Andrew Sent: Tuesday, June 03, 2008 9:26 AM To: Support Desk Subject: RE: regex help The regex will now skip anything with an '@'in the filename on the assumption it's already in the correct format. Uncomment the os.rename line
0
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7619
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7930
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7681
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6290
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5228
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
950
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.