473,322 Members | 1,405 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,322 software developers and data experts.

Regex Class (Regular Expressions)

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 ?
Nov 15 '05 #1
4 4350
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

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Demetri" <Re****@spam.com> wrote in message
news:00****************************@phx.gbl...
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 ?

Nov 15 '05 #2
Eric Gunnerson [MS] <er****@online.microsoft.com> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #3
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.
================================================== =======================

--------------------
Content-Class: urn:content-classes:message
From: "Demetri" <Re****@spam.com>
Sender: "Demetri" <Re****@spam.com>
Subject: Regex Class (Regular Expressions)
Date: Wed, 6 Aug 2003 07:45:12 -0700
Lines: 16
Message-ID: <00****************************@phx.gbl>
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
Thread-Index: AcNcKVctW555ZMt3T+SaHearqZyAyw==
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Newsgroups: microsoft.public.dotnet.languages.csharp
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:174624
NNTP-Posting-Host: TK2MSFTNGXA09 10.40.1.161
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

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 ?


Nov 15 '05 #4
Jon Skeet <sk***@pobox.com> wrote in message news:<MP************************@news.microsoft.co m>...
^(.*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/G...ex-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
Nov 15 '05 #5

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

Similar topics

3
by: Jon Maz | last post by:
Hi All, Am getting frustrated trying to port the following (pretty simple) function to CSharp. The problem is that I'm lousy at Regular Expressions.... //from...
3
by: Senna | last post by:
Hi all, Thru a HttpWebRequest and HttpWebResponse I get html like this(<pre> excluded). <pre> <tr> <td valign="top" align="right" class="SgSVT1BG" nowrap style=" padding-right: 6px "><div...
17
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 ...
8
by: vbmark | last post by:
I'm new to RegEx in vb.net so I'm not sure how to do this. I want to know if a string contains two minus signs "-". If there are two then I want it to return TRUE. I also need to know if the...
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...
3
by: a | last post by:
I'm a newbie needing to use some Regular Expressions in PHP. Can I safely use the results of my tests using 'The Regex Coach' (http://www.weitz.de/regex-coach/index.html) Are the Regular...
1
by: Terry Olsen | last post by:
I download xml logs from several servers every day and read the data out of them using the XmlTextReader. But about 10% of them each day throw exceptions because they are not well formed. I don't...
17
by: Mark | last post by:
I must create a routine that finds tokens in small, arbitrary VB code snippets. For example, it might have to find all occurrences of {Formula} I was thinking that using regular expressions...
4
by: Henrik Dahl | last post by:
Hello! In my application I have a need for using a regular expression now and then. Often the same regular expression must be used multiple times. For performance reasons I use the...
3
by: ommail | last post by:
Hi I wonder if regular expressions are in general sower than using classes like String and Char when used for validating/parsing text data? I've done some simple test (using IsMatch()) method...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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...

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.