Friday, May 27, 2016

Word Boundaries Regex

\b

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.

Implementation

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
{
    [Fact]
    public void ReplaceWords()
    {
        Assert.Equal(
            "This island can has beautiful",
            "This island is beautiful".ReplaceWords("is", "can has"));
 
        Assert.Equal(
            "This island are beautiful",
            "This island is beautiful".ReplaceWords(
                "IS", 
                "are", 
                StringComparison.InvariantCultureIgnoreCase));
    }
}

Enjoy,
Tom

2 comments:

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

    ReplyDelete
  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

    ReplyDelete

Real Time Web Analytics