locked
Problem With Whole Words RRS feed

  • Question

  • Hello Everybody,

    I Am Trying To Find a String In A TextBox With The Regex Function (System.Text.RegularExpressions) To Find A whole Word

    Example Text : "My Dog's  Name Is RexRex Rex"

    If I Made Regex(Rex)

    If Will Give Me The Underlined Rex 

    But What I Mean Is When I Make Regex(Rex) It Gives The Bold Rex Only

    Hope You Got It 

    And Please Give Me the Answer In Vb

    Thanks In Advance,

    Omar Emad

    VB is the Best !!!!!!!!!!!
    Wednesday, September 23, 2009 4:38 AM

Answers

  • I Figured Out The Solution My Self The Regex Pattern Will Be : 
    Dim YourWord As String
    
    Dim MyRegex As New Regex("^([^\S]?[" & YourWord & "][^\S]?)$"
    
    'Hope I Helped


    Hope I Helped By Posting This And Thanks For Trying To Help John And Sirdec

    Omar

    VB is the Best !!!!!!!!!!!
    • Marked as answer by Omar Emad Thursday, September 24, 2009 1:49 AM
    Thursday, September 24, 2009 1:49 AM

All replies

  • Hello

    I think this regex : ".*(?<res>Rex)" can solve your problem.
    But it only take the last "Rex" (and not rex or rEx) and it work for RexRex too but just take the last one.
    Dim test As New Regex(".*(?<res>rex)")
    Dim stringTest = "type Rex in Rex test RexRex"
    Dim result = test.Match(stringTest).Groups("res")
    Wednesday, September 23, 2009 7:52 AM
  • Try this:

    VB
    Dim pattern As String = "(\w+)\1"
    Dim testWords As String = "My Dog's Name Is RexRex Rex, however JohnJohn is my brother"
    Dim rx As New Regex(pattern, RegexOptions.IgnoreCase)
    Dim m As Match = rx.Match(testWords)
    While m.Success
        Console.WriteLine(m.Value)
        m = m.NextMatch()
    End While

    C#
    String pattern = @"(\w+)\1";
    String testWords = "My Dog's Name Is RexRex Rex, however JohnJohn is my brother";
    Regex rx = new Regex(pattern, RegexOptions.IgnoreCase);
    Match m = rx.Match(testWords);
    while (m.Success)
    {
        Console.WriteLine(m.Value);
        m = m.NextMatch();
    }

    //Output
    RexRex
    JohnJohn


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com

    Wednesday, September 23, 2009 1:20 PM
  • Thanks John What I Need Is To Get Rex Not RexRex Please Help If You Know
    VB is the Best !!!!!!!!!!!
    Thursday, September 24, 2009 12:37 AM
  • I Figured Out The Solution My Self The Regex Pattern Will Be : 
    Dim YourWord As String
    
    Dim MyRegex As New Regex("^([^\S]?[" & YourWord & "][^\S]?)$"
    
    'Hope I Helped


    Hope I Helped By Posting This And Thanks For Trying To Help John And Sirdec

    Omar

    VB is the Best !!!!!!!!!!!
    • Marked as answer by Omar Emad Thursday, September 24, 2009 1:49 AM
    Thursday, September 24, 2009 1:49 AM
  • Thanks John What I Need Is To Get Rex Not RexRex Please Help If You Know
    VB is the Best !!!!!!!!!!!

    Why do you have every word in caps?
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Thursday, September 24, 2009 4:13 AM
  • Your requirements were not clear.

    if you want a word then you use boundaries

    \brex\b
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Thursday, September 24, 2009 4:15 AM
  • Dear John

    I Belive That You Are Right I Couldn't Say What I Wanted Exactly And I Wanted To Thank You Again For You Attention

    Thanks Again

    Omar

    VB is the Best !!!!!!!!!!!
    Thursday, September 24, 2009 10:01 AM