Another Problem With Whole Words RRS feed

  • Question

  • I Want To Make Something Like Find Feature In RichTextBox But With Regex I Want To Add The Featur Of : Match Whole Words

    Which Means That When I Try To Match : Omar With Omars It Gives Me True But I Dont Want This

    So Please Anyone Try To Help Me

    Thanks In Advance

    VB is the Best !!!!!!!!!!!
    Saturday, October 17, 2009 3:32 PM


  • Use the "\b" meta-character to match the beginning and ends of the word you want to find. Your pattern would become: "\b WordToFind\b "


    Dim inputs As String() = New String() { "text with Omars in the middle", "text with Omar in the middle" }
    Dim findText As String = "Omar"
    ' escape regex meta-characters such as "." in findText
    Dim pattern As String = "\b" & Regex.Escape(findText) & "\b"
    For Each input As String In inputs 
        Console.WriteLine("{0}: {1}", Regex.IsMatch(input, pattern), input)

    Document my code? Why do you think it's called "code"?
    • Marked as answer by Omar Emad Saturday, October 17, 2009 4:38 PM
    Saturday, October 17, 2009 4:20 PM