473,597 Members | 2,157 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4371
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.co m> 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.*ho und.*)|(.*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.co m>
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.co m>
Sender: "Demetri" <Re****@spam.co m>
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: AcNcKVctW555ZMt 3T+SaHearqZyAyw ==
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
Path: cpmsftngxa06.ph x.gbl
Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1746 24
NNTP-Posting-Host: TK2MSFTNGXA09 10.40.1.161
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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.co m> wrote in message news:<MP******* *************** **@news.microso ft.com>...
^(.*escaped.*ho und.*)|(.*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.*ho und.*)|(.*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.*ho und.*) or (.*hound.*escap ed.*)$. 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.*ho und.*|.*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.*ho und|hound.*esca ped).*$

It places the whole matched row in group 0 and the "escaped.....ho und" 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
2071
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 http://support.microsoft.com/default.aspx?scid=kb;EN-US;246800 function fxnParseIt() { var sInputString = 'asp and database';
3
1712
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 class="SgZText"><span
17
3957
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/
8
1816
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 string contains two plus signs "+". Should this be a seperate RegEx or can one RegEx check for both signs? Thanks!
7
2222
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( _ "(+)|" & _ "(+)|" & _ "(+)|" & _ "(+)|" & _ "(+)|" & _
3
3012
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 Expressions used in Perl identical to the Regular Expressions in PHP?
1
1538
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 want to lose the data in the files that won't load into an XmlDocument. So I was thinking maybe using a RegEx function, sending a Node Name to the function and having it return the InnerText. Is this a good use for RegEx, or is there a better...
17
2777
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 might be a neat way to solve this, but I am new to them. Can anyone give me a hint here? The catch is, it must only find tokens that are not quoted and not commented; examples follow
4
2176
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 RegexOptions.Compiled when I instantiate it. It must be obvious that it takes some time to instantiate such an object. My question is, does the Regex instantiation somehow deal with some caching internally so instantiating a Regex object multiple times...
3
2290
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 and the result was that Regex is either as fast or two times slower than method which used methods from classes
0
7965
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8271
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8380
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8258
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6686
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5847
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5426
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3881
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3923
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.