473,407 Members | 2,312 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,407 software developers and data experts.

RegEx Question

I need help creating a regular expression which will match the following:

"foo" anywhere in the text
Also the text should contain either "bar" or "bar1"

Following are some examples of text which should return a match:

xxxx foo xxxx xxx bar xxxx
xxxx foo xxxx xxx bar1 xxxx
xxxx bar xxxxxxx foo xxxxx xxxx
xxxx bar1 xxxxxxx foo xxxxx xxxx
xxxx bar1 xxxx bar xxx foo xxxxx xxxx
xxx foo xxxxx xxxx xxxx bar1 xxxx bar

Thanks,
Scott
Nov 15 '05 #1
3 1019
Scott:

Do you want to match the whole line, just the foo and the bar or bar1, all
the xxx's between them? And what to do if bar and bar1 appear in the same
line (one foo, one bar and one bar1?).
"Scott Turner" <sc***@prorege.com> wrote in message
news:d3**************************@posting.google.c om...
I need help creating a regular expression which will match the following:

"foo" anywhere in the text
Also the text should contain either "bar" or "bar1"

Following are some examples of text which should return a match:

xxxx foo xxxx xxx bar xxxx
xxxx foo xxxx xxx bar1 xxxx
xxxx bar xxxxxxx foo xxxxx xxxx
xxxx bar1 xxxxxxx foo xxxxx xxxx
xxxx bar1 xxxx bar xxx foo xxxxx xxxx
xxx foo xxxxx xxxx xxxx bar1 xxxx bar

Thanks,
Scott

Nov 15 '05 #2

If you want to match the "foo" part only, use this:
((?<=(bar|bar1).*)foo|foo(?=.*(bar|bar1)))

If you want to match the whole line, use this:
..*(bar|bar1).*foo.*|.*foo.*(bar|bar1).*

If you don't really need the matched text and just need to know if there is
a match, use the IsMatch function with either of these.

Also, I am assuming that you really aren't going to be looking for the
literals "bar" or "bar1". If for some reason you were, then you would only
need to look for "bar" since it is contained within "bar1".

Brian Davis
www.knotdotnet.com


"Scott Turner" <sc***@prorege.com> wrote in message
news:d3**************************@posting.google.c om...
I need help creating a regular expression which will match the following:

"foo" anywhere in the text
Also the text should contain either "bar" or "bar1"

Following are some examples of text which should return a match:

xxxx foo xxxx xxx bar xxxx
xxxx foo xxxx xxx bar1 xxxx
xxxx bar xxxxxxx foo xxxxx xxxx
xxxx bar1 xxxxxxx foo xxxxx xxxx
xxxx bar1 xxxx bar xxx foo xxxxx xxxx
xxx foo xxxxx xxxx xxxx bar1 xxxx bar

Thanks,
Scott

Nov 15 '05 #3
Thanks Brian!

I am using the static IsMatch method, and had originally created a
RegEx much like the ones you suggested. However, when looping through
articles and testing for matches, it is much slower than what I need.

If I can't find a faster way to test for a match, I will just have to
re-architect things.

Scott

"Brian Davis" <br***@knowdotnet.nospam.com> wrote in message news:<ei**************@TK2MSFTNGP10.phx.gbl>...
If you want to match the "foo" part only, use this:
((?<=(bar|bar1).*)foo|foo(?=.*(bar|bar1)))

If you want to match the whole line, use this:
.*(bar|bar1).*foo.*|.*foo.*(bar|bar1).*

If you don't really need the matched text and just need to know if there is
a match, use the IsMatch function with either of these.

Also, I am assuming that you really aren't going to be looking for the
literals "bar" or "bar1". If for some reason you were, then you would only
need to look for "bar" since it is contained within "bar1".

Brian Davis
www.knotdotnet.com


"Scott Turner" <sc***@prorege.com> wrote in message
news:d3**************************@posting.google.c om...
I need help creating a regular expression which will match the following:

"foo" anywhere in the text
Also the text should contain either "bar" or "bar1"

Following are some examples of text which should return a match:

xxxx foo xxxx xxx bar xxxx
xxxx foo xxxx xxx bar1 xxxx
xxxx bar xxxxxxx foo xxxxx xxxx
xxxx bar1 xxxxxxx foo xxxxx xxxx
xxxx bar1 xxxx bar xxx foo xxxxx xxxx
xxx foo xxxxx xxxx xxxx bar1 xxxx bar

Thanks,
Scott

Nov 15 '05 #4

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

Similar topics

4
by: engwar1 | last post by:
Not sure where to ask this. Please suggest another newsgroup if this isn't the best place for this question. I'm new to both vb.net and regex. I need a regular expression that will validate what...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
2
by: Tim Conner | last post by:
Hi, Thanks to Peter, Chris and Steven who answered my previous answer about regex to split a string. Actually, it was as easy as create a regex with the pattern "/*-+()," and most of my string...
6
by: Du Dang | last post by:
Text: ===================== <script1> ***stuff A </script1> ***more stuff <script2> ***stuff B
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 ...
5
by: Chris | last post by:
How Do I use the following auto-generated code from The Regulator? '------------------------------------------------------------------------------ ' <autogenerated> ' This code was generated...
6
by: Martin Evans | last post by:
Sorry, yet another REGEX question. I've been struggling with trying to get a regular expression to do the following example in Python: Search and replace all instances of "sleeping" with "dead"....
7
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...
6
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...
6
by: | last post by:
Hi all, Sorry for the lengthy post but as I learned I should post concise-and-complete code. So the code belows shows that the execution of ValidateAddress consumes a lot of time. In the test...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
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...

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.