locked
Regular expression RRS feed

  • Question

  • User527076549 posted

    Hi,

    i want to match exact string

    example to match "test" in below

    "test"  - ismatch true

    "test new" -true

    "new test"- true

    "testtt"- false

    this is the expression

    Regex.IsMatch("input", @"/\b(test)\b/") -> it always gives false any idea ??

    Monday, July 20, 2020 5:21 PM

Answers

  • User-821857111 posted

    Looks like you might have copied the pattern from a JS regex example. You don't need the escape characters:

    var inputs = new[]{"test","test new","new test","testtt"};
    foreach(var input in inputs)
    {
        Console.WriteLine(Regex.IsMatch(input, @"\b(test)\b"));
    }
    

    Note also that your example tests the string "input", which will always result in false.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 20, 2020 5:54 PM