match word using regular expression [Resolved]

Posted by Alok under C# on 4/15/2011 | Points: 10 | Views : 5013 | Status : [Member] | Replies : 3
i have doc file containning word "i likes dotnetfunda ".
i want that when i serch for 'dotnetfunda' then we sould get result true .if i serch for 'dot' or 'dotnet' then result should be false.. to match exact word using regularexpression

Thanks & Regards,


Posted by: Bugwee on: 4/17/2011 [Member] Starter | Points: 50


Here's a sample code using Try to use or any to convert this code into C#.

Private Function MatchWord(ByVal txtSearch As String) As Boolean

MatchWord = False

Dim word As String = "i like dotnetfunda"
Dim pattern As String = "[a-zA-Z]+"

'create a new RegEx object
Dim reg As New Regex(pattern, RegexOptions.IgnoreCase Or RegexOptions.ExplicitCapture)

'put all the matches into a MatchCollection
Dim matches As MatchCollection = reg.Matches(word)

'loop through all the matches
For Each match As Match In matches
For Each group As Group In match.Groups

If group.Value = txtSearch Then
MatchWord = True
End If

Return MatchWord

Catch ex As Exception
Return Nothing
End Try

End Function

Alok, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Umeshdwivedi on: 4/15/2011 [Member] Starter | Points: 25

Hi !!!!!!!!!!!!!!!!!!!!!!!
If a line can meet any out of series of requirements, simply use alternation in the regular expression. ^.*\b(one|two|three)\b.*$ matches a complete line of text that contains any of the words "one", "two" or "three". The first backreference will contain the word the line actually contains. If it contains more than one of the words, then the last (rightmost) word will be captured into the first backreference. This is because the star is greedy. If we make the first star lazy, like in ^.*?\b(one|two|three)\b.*$, then the backreference will contain the first (leftmost) word.

If a line must satisfy all of multiple requirements, we need to use lookahead. ^(?=.*?\bone\b)(?=.*?\btwo\b)(?=.*?\bthree\b).*$ matches a complete line of text that contains all of the words "one", "two" and "three". Again, the anchors must match at the start and end of a line and the dot must not match line breaks. Because of the caret, and the fact that lookahead is zero-width, all of the three lookaheads are attempted at the start of the each line. Each lookahead will match any piece of text on a single line (.*?) followed by one of the words. All three must match successfully for the entire regex to match. Note that instead of words like \bword\b, you can put any regular expression, no matter how complex, inside the lookahead. Finally, .*$ causes the regex to actually match the line, after the lookaheads have determined it meets the requirements.

If your condition is that a line should not contain something, use negative lookahead. ^((?!regexp).)*$ matches a complete line that does not match regexp. Notice that unlike before, when using positive lookahead, I repeated both the negative lookahead and the dot together. For the positive lookahead, we only need to find one location where it can match. But the negative lookahead must be tested at each and every character position in the line. We must test that regexp fails everywhere, not just somewhere.

Finally, you can combine multiple positive and negative requirements as follows: ^(?=.*?\bmust-have\b)(?=.*?\bmandatory\b)((?!avoid|illegal).)*$ . When checking multiple positive requirements, the .* at the end of the regular expression full of zero-width assertions made sure that we actually matched something. Since the negative requirement must match the entire line, it is easy to replace the .* with the negative test.

Latest Technology Trainer
And Part time software consultant

Alok, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 4/18/2011 [Member] Starter | Points: 25

Hi Alok,

Please take a look at the bellow code.
Take the string and split in to words. Then Compare your required string.

String str = "abc sda nnndd abC. hELLO ABC 123 ABCDEF AB AB. aB 12345.";

var words=str.Split(new char[]{' ','.',',',';',':'});
var abcwords = words.Where(w => w.Equals("abc", StringComparison.OrdinalIgnoreCase));
foreach (var item in abcwords)


Alok, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response