I am simply trying to find all instances of a word in a richtextbox.text with the following
private void FindAllReferences(RichTextBox rtb, string sText)
{
MatchCollection mcReferences;
mcReferences = Regex.Matches(rtb.Text, sText, RegexOptions.IgnoreCase);
}
The above seems like it should work, and the values passed to the method are as expected; i.e. no extra characters or whitespace.
I've tried...
@"\<" + sText + @"\>"
and
@"." + sText + @"."
I am quite new to Regex, so this problem is not surprising, however, I've managed to get some far more complex patterns working properly, like the following
//Build pattern
StringBuilder stbPattern = new StringBuilder(sFuncName);
MatchCollection mcFunction;
stbPattern.Append(iNumParams > 0 ? @"\((\s?\w+\s?,\s?){" + --iNumParams + @"}\s?\w+\s?\)\s?\{" : @"\s?\{");
mcFunction = Regex.Matches(rtb.Text, stbPattern.ToString());
Works wonderfully in finding a function definition named sFuncName in the text of a scripted language. iNumParams is calculated prior to this segment of code of course.
So I am perplexed.
I just want to match some simple words.

1 answers
To match words, you need to use the word boundary character \b:
answered 4 months ago by:
11603
117
Thanks, although I should have mentioned I tried that also, it turns out that it wasn't the problem. I was breaking at the closing '}' to check the value in mcReferences. Since mcReferences went out of scope at that point; it was naturally empty. Using the 'sText' as is, with no additional pattern matching is what I want, since I want it to match all occurences. I want it to match fred, frederick and alfred for 'fred'.