Regex Class (Regular Expressions)

  • Thread starter Thread starter Demetri
  • Start date Start date
D

Demetri

Lets pretend you have a string array and each element is a
sentence. Two of those elements read as follows:

1. The fox escaped from the hound.
2. The fox almost escaped.

Now lets say you loop the string array and inside that
loop you use a Regex class to determine if the current
element matches a certain criteria. The criteria being as
follows:

I want any string which contains the word escaped AND the
word hound in it.

What reg exp pattern would return a positive match on the
element above labled as number 1 ?
 
Eric Gunnerson said:
This matches the first one:

escaped.+hound

but you need it in the other order as well, so the one I'd use is:

escaped.+hound|hound.+escaped

That appears to work fine. I tried to answer this post earlier, but
using the somewhat more clumsy:

^(.*escaped.*hound.*)|(.*hound.*escaped.*)$

That doesn't work (it doesn't match "Test with escaped then hound in
it") and I don't quite understand why. Any chance you could enlighten
me?
 
If you want the element (string) which contains the word "escaped" AND the
word "hound" in it, then according to your two strings provided, regex
would not return a positive number at all. since neither string has both
words in it.

Cheers,
Christian T. [MSFT]
Visual Studio Update Team

- Please do not reply to this email directly. This email is for newsgroup
purposes only.
=========================================================================
This posting is provided "AS IS" with no warranties, and confers no
rights. Use of included script samples are subject to the terms specified
at http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which
they originated.
=========================================================================

--------------------
 
Jon Skeet said:
^(.*escaped.*hound.*)|(.*hound.*escaped.*)$

That doesn't work (it doesn't match "Test with escaped then hound in
it") and I don't quite understand why. Any chance you could enlighten
me?

Possible bug in the .net regex engine? It parsed correctly using a java
applet I found at

http://www.cis.upenn.edu/~matuszek/General/RegexTester/regex-tester.html

Also in the .net regex engine the following regex seems to parse correctly

^(.+escaped.*hound.*)|(.*hound.*escaped.*)$

The regex probably doesn't really have the meaning you thought it had
though. Misplaced paranthesises causes the regex to match on either
^(.*escaped.*hound.*) or (.*hound.*escaped.*)$. Placing the whole matched
string in group 0. Group 1 or 2 will also contain the full string while
the other group will be null since only one of the two paths can be
followed.

^(.*escaped.*hound.*|.*hound.*escaped.*)$ would make the regexp a little
less complex although this expression also seems to fail in .net regex
engine.

The most practical regex if you want to use ^,$ to match the
whole row is probably

^.*(escaped.*hound|hound.*escaped).*$

It places the whole matched row in group 0 and the "escaped.....hound" in
group 1. This regex does work in the .net regex engine.

/Marcus
 
Back
Top