Regular expression questions

  • Thread starter Thread starter siddharthkhare
  • Start date Start date
S

siddharthkhare

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+)SecondWord)");


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
 
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
 
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
 
Back
Top