Regular Expression (RegEx) to Find Whole Words in a String

Ever want to match and replace whole words with in a string?  Regular expressions (System.Text.RegularExpressions) makes it a one line operation:

Regex.Replace(inputText, @"\b" + wordToReplace + @"\b", replacementText, RegexOptions.IgnoreCase);

This pattern uses “word boundries” as your delimiters for matching text.

This entry was posted in C#, Regular Expressions. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>