Monday, January 25, 2010

Removing a word containing a token: RegEx Again

So you want to remove whole words contain a give token...here is a nice little RegEx way to do this.

Lets remove words with the token "ug" in the following string.

Here is a code snippet to do this in C#

string test = "Break Plug dug on through slugit to the 233bugme other side";
string regExExpression = @"\S*ug\S*"; //This is the brains of the process...

string result = Regex.Replace(test, regExExpression, "");

If you write the result to the console you will get:

Break on through to the other side

PS I dont know if this Regex also grabs capital "UG" tokens?

Fun, Fun...
QuiJon

0 comments:

Post a Comment