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

Regular expression questions

Hi All,
I want to capture a word which is in certain range from another word
only if a third word is not between them(anywhere in the range).

Regex r = new
Regex(@"(FirstWord.{1,25})*((?<!ThirdWord\s+)Secon dWord)");
So it works fine for a string like this
FirstWord ThirdWord SecondWord
it works fine in this case and does not return me a match.
but lets say string is of this form
FirstWord ThirdWord SometingElse here SecondWord
than returns me a match .I wan it to not return me anything if there is

a "ThirdWord" any where in the that range.
Irrespective if it is just before "SecondWord" or any where else in
that range.
Thanks
KS

Mar 31 '06 #1
4 991
I think this is what you are looking for:
FirstWord(?!.*ThirdWord).{1,25}SecondWord

This way you match FirstWord first. The look ahead then checks to see if there are any characters
ahead of it that also contain the word ThirdWord. If it finds ThirdWord with the optional
preceeding characters it imediately fails (actually it will look again for another instance of
FirstWord and then fail). If it doesn't fail it will then goes back to the end of FirstWord and
then continues from there. The next check is to see if you have your range of 1-25 characters
followed by SecondWord. If they are there then it succeeds.

Tome
http://www.pcdotcom.com

On 30 Mar 2006 20:57:22 -0800, si************@hotmail.com wrote:
Hi All,
I want to capture a word which is in certain range from another word
only if a third word is not between them(anywhere in the range).

Regex r = new
Regex(@"(FirstWord.{1,25})*((?<!ThirdWord\s+)Seco ndWord)");
So it works fine for a string like this
FirstWord ThirdWord SecondWord
it works fine in this case and does not return me a match.
but lets say string is of this form
FirstWord ThirdWord SometingElse here SecondWord
than returns me a match .I wan it to not return me anything if there is

a "ThirdWord" any where in the that range.
Irrespective if it is just before "SecondWord" or any where else in
that range.
Thanks
KS

Mar 31 '06 #2
Sorry, one quick change:
FirstWord(?!.{1,25}?ThirdWord).{1,25}SecondWord/

The way I originally sent it would return false with a string like this:
"FirstWord blah SecondWord ThirdWord"
The above fixes that by changing the look ahead to use:
..{1,25}?
instead of
..*

Tome
http://www.pcdotcom.com

On Fri, 31 Mar 2006 10:51:28 -0500, Tome <to**@pcdotcom.com> wrote:
I think this is what you are looking for:
FirstWord(?!.*ThirdWord).{1,25}SecondWord

This way you match FirstWord first. The look ahead then checks to see if there are any characters
ahead of it that also contain the word ThirdWord. If it finds ThirdWord with the optional
preceeding characters it imediately fails (actually it will look again for another instance of
FirstWord and then fail). If it doesn't fail it will then goes back to the end of FirstWord and
then continues from there. The next check is to see if you have your range of 1-25 characters
followed by SecondWord. If they are there then it succeeds.

Tome
http://www.pcdotcom.com

On 30 Mar 2006 20:57:22 -0800, si************@hotmail.com wrote:
Hi All,
I want to capture a word which is in certain range from another word
only if a third word is not between them(anywhere in the range).

Regex r = new
Regex(@"(FirstWord.{1,25})*((?<!ThirdWord\s+)Sec ondWord)");
So it works fine for a string like this
FirstWord ThirdWord SecondWord
it works fine in this case and does not return me a match.
but lets say string is of this form
FirstWord ThirdWord SometingElse here SecondWord
than returns me a match .I wan it to not return me anything if there is

a "ThirdWord" any where in the that range.
Irrespective if it is just before "SecondWord" or any where else in
that range.
Thanks
KS

Mar 31 '06 #3
thanks Tome

Apr 1 '06 #4
thanks Tome

Apr 1 '06 #5

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

Similar topics

7
by: Patient Guy | last post by:
Coding patterns for regular expressions is completely unintuitive, as far as I can see. I have been trying to write script that produces an array of attribute components within an HTML element. ...
3
by: Joe | last post by:
Hi, I have been using a regular expression that I don’t uite understand to filter the valid email address. My regular expression is as follows: <asp:RegularExpressionValidator...
18
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
8
by: Michael Primeaux | last post by:
I'm somewhat new to regular expressions and am in need of assistance. I'd like to match on any OBJECT, EMBED, or APPLET element nested inside an <HTML> element. Would someone point me in the right...
15
by: Mark Rae | last post by:
Hi, I'm trying to construct a RegEx pattern which will validate a string so that it can contain: only the numerical characters from 0 to 9 i.e. no decimal points, negative signs, exponentials...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
3
by: Zeba | last post by:
Hi guys, I need some help regarding regular expressions. Consider the following statement : System.Text.RegularExpressions.Match match =...
5
by: laredotornado | last post by:
Hi, I have a span that contains text of the form var spanHtml = "My Tab Content (7)"; The content is guaranteed to end with a string of the form "(#)" where "#" is a whole number. My...
12
by: joey.powell | last post by:
Hello guys, I am primarily an asp.net programmer, but lately I've been doing some work with Apache, MySQL and PHP. Anyways, I have a question that I posted in the microsoft asp.net newsgroup....
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
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?
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
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
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,...

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.