A.D. A.D. - 14 days ago 6
C# Question

How to match boundary \b with accents

When I test on https://regex101.com/, if I leave standard options (/i)
pattern

\bécumé\b


will NOT find a match in text:

123 écumé 456


However, the match will be found if I add unicode flag:

/iu


How can I do that in C#? The following does find a match:

string pattern = "/\bécumé\b/iu"

Answer

As pointed out by @Callum, Regex101 does not support C#. If you try it in C#, it does work:

    [Test]
    public void TestMatch()
    {
        // Arrange.
        const string example = "123 écumé 456";

        Regex pattern = new Regex(@"\bécumé\b");

        // Act.
        Match match = pattern.Match(example);

        // Assert.
        Assert.That(match.Success, Is.True);
    }

Also to point out that when you say

the following does not find a match: "/\bécumé\b/iu"

The "/iu" in the string is not doing what you might think: in C# you can give regex options using a different parameter, not as part of the pattern string. For example:

Regex pattern = new Regex(@"\bécumé\b", RegexOptions.IgnoreCase);