Friday, May 27, 2016

Word Boundaries Regex


This is the second time this week where I have had to ask myself "how did I not know about this?"

There is a regex character to identify word boundaries: \b This is a zero length match, similar to the caret and dollar sign. It finds the boundaries between words, allowing you to search for a whole word match.

Below is a sample extension method that uses this to replace words in a string.


public static class StringExtensions
    private static readonly Regex WordRegex = new Regex(@"\b\w+\b", RegexOptions.Compiled);
    public static string ReplaceWords(
        this string input,
        string find,
        string replace,
        StringComparison comparison = StringComparison.InvariantCulture)
        return WordRegex.Replace(input, m => m.Value.Equals(find, comparison)
            ? replace
            : m.Value);

Unit Test

public class StringExtensionsTests
    public void ReplaceWords()
            "This island can has beautiful",
            "This island is beautiful".ReplaceWords("is", "can has"));
            "This island are beautiful",
            "This island is beautiful".ReplaceWords(



  1. Great Information. Very useful. Thanks for sharing.

  2. 8 Casino City, New York, United States - Mapyro
    See 광양 출장마사지 8 Casino City, 밀양 출장안마 New York, United 문경 출장샵 States map and reviews. 6th 아산 출장샵 Street in the Empire State Plaza 제주 출장마사지 Hotel and Casino at  Rating: 3 · ‎4 votes


Real Time Web Analytics