-4

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"
A.D.
  • 898
  • 8
  • 28
  • 1
    Regex101 doesn't support C# regex yet!! – Callum Linington Nov 24 '16 at 13:21
  • 2
    [Cannot reproduce](http://ideone.com/N2KPdC). You don't need the slashes (delimiters) in C# expression strings (`/regex/`). Also, read [ask] and elaborate on "does not work". – CodeCaster Nov 24 '16 at 13:24
  • 1
    `\b` and all other shorthand classes (like `\w`, `\d`, `\s`) are Unicode aware **by default** in .NET. Use plain `string pattern = @"\bécumé\b"`. – Wiktor Stribiżew Nov 24 '16 at 13:30
  • 1
    Do not blindly follow what online testing sites show: always test in the target environment. Besides, use *verbatim string literals* when defining regular expression patterns, `@"..."`, to avoid situations like this when you tried to match a *backspace*, not a word boundary, with `"\b"`. – Wiktor Stribiżew Nov 24 '16 at 13:38

2 Answers2

1

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);

Richardissimo
  • 5,083
  • 2
  • 12
  • 32
-2

Regex101 has an option where you can see the c# code... Try this (took it from the site):

    using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string pattern = @"\bécumé\b";
        string input = @"123 écumé 456";
        RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.CultureInvariant;

        foreach (Match m in Regex.Matches(input, pattern, options))
        {
            Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
        }
    }
}
Renato Afonso
  • 656
  • 6
  • 13
  • "Try this" is not an answer. Explain what is different from the OP's code and why that solves their problem. – CodeCaster Nov 24 '16 at 13:30
  • thx Renato for pointing me out the C# samples in Regex101. I had never seen this part. However, their C# sample (that you pasted) actually does not find the match. – A.D. Nov 24 '16 at 13:35