473,385 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Regex items in any order

I've done a bit of research on this topic in the newsgroups and on MSDN, and though it sounds possible, I still don't understand how I would make it work.

I want to be able to use named groups to capture from a path items that can be in any order.

For example, the path might be:

/root/item1/item2/item2/item3/item4

or it might be mixed up:

/root/item4/item3/item1/item2

and it is possible that certain items might not even be present:

/root/item3/item1

/root will always be present, though its name will vary depending on which type of path I am trying to match--so I'm mainly concerned with matching named groups <Item1through <Item4when they can appear in any order.

I'd prever to be able to do this WITHOUT needing to write an expression for each combination, as there could be upwards of 10 items I want to match (too many combinations!!)

Thanks ahead of time for your help!

--
Greg Collins [Microsoft MVP]
Visit Braintrove ( http://www.braintrove.com )
Dec 20 '06 #1
9 3519
Hi Greg,

It's probably easier to just split the path into an array on the "/"
character, then iterate the array and for each element check whether it
exists in a list of values.

--
Dave Sexton

"Greg Collins [Microsoft MVP]" <gcollins_AT_msn_DOT_comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
I've done a bit of research on this topic in the newsgroups and on MSDN, and
though it sounds possible, I still don't understand how I would make it
work.

I want to be able to use named groups to capture from a path items that can
be in any order.

For example, the path might be:

/root/item1/item2/item2/item3/item4

or it might be mixed up:

/root/item4/item3/item1/item2

and it is possible that certain items might not even be present:

/root/item3/item1

/root will always be present, though its name will vary depending on which
type of path I am trying to match--so I'm mainly concerned with matching
named groups <Item1through <Item4when they can appear in any order.

I'd prever to be able to do this WITHOUT needing to write an expression for
each combination, as there could be upwards of 10 items I want to match (too
many combinations!!)

Thanks ahead of time for your help!

--
Greg Collins [Microsoft MVP]
Visit Braintrove ( http://www.braintrove.com )

Dec 20 '06 #2
Hello Greg,

I'm not entirely sure I understand what you want to do. For instance, this
expression here matches all your examples and allows you to get to each
single element of the path:

\/[a-z0-9]+

Of course this could be a bit more static, leaving less options for the
names of path elements... maybe like this:

\/(root|item\d)

This matches all your samples as well.

From your question I'm getting the impression that you want to do more
than this, but I don't really understand what exactly you need. Can you
explain a bit more on this basis?
Oliver Sturm
--
http://www.sturmnet.org/blog
Dec 20 '06 #3
Thanks. This sounds like it might be a viable option... I'll look into this.

--
Greg Collins [Microsoft MVP]
Visit Braintrove ( http://www.braintrove.com )
Dec 20 '06 #4
I was using a pretty generic sample... So if I follow down one particluar root, in this example: articles, i might have the following possibilities for valid paths:

/articles
/articles/new
/articles/updated
/articles/coming
/articles/author/{author}
/articles/date/{year}[/{month}[/{day}]]
/articles/level/{level}
/articles/mrv
/articles/mfv

Most of these can be combined in any order. So I might have paths like:

/articles/level/3/author/Greg/new
/articles/date/2006/12/level/2-4/
/articles/author/Greg/mfv/

etc.

Hope that helps.

--
Greg Collins [Microsoft MVP]
Visit Braintrove ( http://www.braintrove.com )
Dec 20 '06 #5
Hello Greg,
>I was using a pretty generic sample... So if I follow down one particluar
root, in this example: articles, i might have the following possibilities
for valid paths:
<snip>

Well, all of these should be matched by the first of the two alternatives
I posted. Have you tried it?

My previous post was targeted more at additional functionality that I
thought you wanted, not necessarily at additional samples. IOW, from your
original post I got the impression that you weren't asking for somebody to
write a regular expression for you, but rather to solve a particular
problem you were having, beyond writing the expression in the first place.

Finally, what Dave said in the parallel thread may very well be true - if
all you know about your path is that it consists of elements that are
separated by slash characters, you might get a more efficient algorithm by
splitting first and analyzing second, if analyzing is required at all. As
for as I understand, you don't have any syntactical requirements for the
path elements at all (except maybe that they can't contain the slash
character), in which case the second step could fall away completely.

The regular expression for this case would be (if you decide to go that way)

\/[^\/]+
Oliver Sturm
--
http://www.sturmnet.org/blog
Dec 20 '06 #6
Hi Oliver, thanks for your help! I haven't had time to play with any of the solutions yet. I expect I will have time later today or tomorrow to do that.

--
Greg Collins [Microsoft MVP]
Visit Braintrove ( http://www.braintrove.com )
Dec 20 '06 #7
Hi Greg,

I assumed that you wanted something like Oliver's second example but with a
variable number of possible elements (upwards of 10 was stated):

/(new|date|coming|updated|author|(\d{4}(\d{2}(\d{2} )?)?))

Calling Regex.Matches using the expression above might work (I didn't test
it myself); however, if there are a variable number of elements then I
suggest splitting and iterating instead since that will be much less cryptic
than dynamically creating a Regex pattern, IMO.

Also, if it's really "named groups" that you're interested in then you might
need to iterate the Matches anyway. i.e., If each of "new", "date",
"coming", etc. have specific semantics that your application must be able to
identify then using a Regex is probably not the best approach, especially if
the number of possible elements may vary.

--
Dave Sexton

"Greg Collins [Microsoft MVP]" <gcollins_AT_msn_DOT_comwrote in message
news:uy*************@TK2MSFTNGP03.phx.gbl...
Thanks. This sounds like it might be a viable option... I'll look into this.

--
Greg Collins [Microsoft MVP]
Visit Braintrove ( http://www.braintrove.com )

Dec 20 '06 #8
Thanks Dave.

I think I prefer splitting the string to using a generic regex. One thing I also need to do is identify when there is an invalid path and then take appropriate measures. Although both the generic regex and the split string would both ultimately get me to the same set of data, I think it would be easier to work with the split string in the long run (at least for me).

Thank both you and Oliver for your assistance. It would be great if regex had a way of doing what I was wanting (random order matches) but since it doesn't, I'll just have to hack it out of the string! :o)

--
Greg Collins [Microsoft MVP]
Visit Braintrove ( http://www.braintrove.com )
Dec 21 '06 #9
Hello Greg,
>Thank both you and Oliver for your assistance. It would be great if regex
had a way of doing what I was wanting (random order matches) but since it
doesn't, I'll just have to hack it out of the string! :o)
Hm... that's pretty amazing. Since my first reply I've been trying to
understand what you were getting at with that "random order match"
request, but I haven't understood it yet :-) If you can explain, I'll have
another shot.
Oliver Sturm
--
http://www.sturmnet.org/blog
Dec 21 '06 #10

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

Similar topics

2
by: Daniel Billingsley | last post by:
First, if MSFT is listening I'll say IMO the MSDN material is sorely lacking in this area... it's just a whole bunch of information thrown at you and you're left to yourself as to organizing it in...
3
by: Luis Esteban Valencia | last post by:
hello quite a simple one if you understand regular expressions vbscript and ..net, probably quite hard if you don't i have a single line input which offers classic search functionality, so if...
6
by: tshad | last post by:
Is there a way to use Regex inside of a tag, such as asp:label? I tried something like this but can't make it work: <asp:label id="Phone" text=Regex.Replace('<%# Container.DataItem("Phone")...
7
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...
13
by: Chris Lieb | last post by:
I am trying to write a regex that will parse BBcode into HTML using JavaScript. Everything was going smoothly using the string class replace() operator with regex's until I got to the list tag....
4
by: luca.milan | last post by:
Hi, have a problem with this regex: ^(?<target>order|customer)@(?<action>view|save|delete)$ With this options: order@view, order@save, order@delete or customer work fine, but with the target:...
11
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend...
7
by: Nightcrawler | last post by:
Hi all, I am trying to use regular expressions to parse out mp3 titles into three different groups (artist, title and remix). I currently have three ways to name a mp3 file: Artist - Title ...
7
by: =?Utf-8?B?amFj?= | last post by:
Hi, I have problems with following code and don’t find the bug : // Set ArrayList aArray = new ArrayList(); regStr = new Regex(@"\?)*(\d+)\]"); if(text != null && regStr.IsMatch(text))...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.