.NET Framework Bookmark and Share   
 index > Regular Expressions > Problem With Whole Words
 

Problem With Whole Words

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 IsRexRexRex"

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 !!!!!!!!!!!
Omar Emad
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 byOmar Emad 6 hours 36 minutes ago
  •  
Omar Emad
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)")<br/>
Dim stringTest = "type Rex in Rex test RexRex"<br/>
Dim result = test.Match(stringTest).Groups("res")<br/>

Sirdec

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'sName 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

JohnGrove
Thanks John What I Need Is To Get Rex Not RexRex Please Help If You Know
VB is the Best !!!!!!!!!!!
Omar Emad
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 byOmar Emad 6 hours 36 minutes ago
  •  
Omar Emad
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
JohnGrove
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
JohnGrove

You can use google to search for other answers

Custom Search

More Threads

• Exception from HRESULT: 0x800A03EC
• Is there a way to have a named group stated ONCE and refer to it in the regex?
• Finding instances of unicode character \u00FF
• need regular expression to not include strings contains a special charchters
• How can i match this text with a regex?
• Regular epressions union
• Regex for blank spaces only.
• is this ridiculous use of regex?
• Help me in creating appropriate RegEx
• SplittingaLongUrl