﻿# Finding words NOT in  a piece of text

<datetime class="hidden">2004-01-28T00:00</datetime>
<!-- category -- mostlylucidcouk, Imported, C#, Software Development -->

Pretty clever little function...can't think of a use for it right now :-)


## [Find words NOT IN text](http://weblogs.asp.net/dneimke/posts/26454.aspx) 

I'm currently writing a series of articles (that will hopefully get published {grin}) titled "Regex Reminders" that will provide dozens of code snippets that demonstrate how to perform common - and some not-so-common - operations using regex.

In the first article titled "Replacing" I came up with a script that shows how to highlight a word when NOT found within another piece of text.  The following code snippet highlights that...

***Match "foo" not in ANCHORS using MatchEvaluator***

> 
> 
> **[C#]**
> string source = sourceTextBox.Text ;
> Regex re = new Regex( @"(?'Url'&lt;a [^&gt;]\*&gt;.\*?&lt;/a&gt;)|(?'theWord'foo)" ) ;
> // use a MatchEvaluator with a pointer to the delegate method
> string result = re.Replace( source, new MatchEvaluator( FormatLinkBits ) ) ;
> 
> 
> // delegate method
> private string FormatLinkBits( Match m )
> {
>     if( m.Groups["theWord"].Success ) 
>     {
>         string theWord = m.Groups["theWord"].Value ;
>         return "&lt;b&gt;" + theWord + "&lt;/b&gt;" ;
>     }
>     else
>         return m.Value ;
> }
> 

This would be useful for things like turning words into hyperlinks - similar to the "[KeyWords" feature that ScottW recently implemented into version 0.94 of .Text](http://scottwater.com/dottext/posts/9738.aspx).