HI all:
i've tried to make a simple notepad in c# but i want to know how can i find a word in the text (i'm using Rich text box)
pleas help me???
HI all:
i've tried to make a simple notepad in c# but i want to know how can i find a word in the text (i'm using Rich text box)
pleas help me???
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
AlanPosted Mar 21, 2008, 8:25 PM
To find embedded words as well as complete words, just remove the two word boundary metacharacters \b from the regular expression:
Regex r = new Regex( word , RegexOptions.IgnoreCase);
Omar MardiniPosted Mar 21, 2008, 5:16 PM
Thanks Alan it works but if u can post how can i find a part of word and mark the word wich i found
thanks
AlanPosted Mar 20, 2008, 12:33 PM
To find only complete words (as opposed to words embedded in other words) regular expressions are your best option. Here's some code which looks for all occurrences (ignoring case) within the RTB of a word entered in an (ordinary) textbox and then emboldens them:
using
System.Text.RegularExpressions;.......
string
word = textBox1.Text; // say Regex r = new Regex(@"\b" + word + @"\b", RegexOptions.IgnoreCase); MatchCollection mc = r.Matches(richTextBox1.Text); foreach (Match m in mc){
richTextBox1.SelectionStart = m.Index;
richTextBox1.SelectionLength = word.Length;
richTextBox1.SelectionFont =
new Font(richTextBox1.Font, FontStyle.Bold);richTextBox1.SelectionLength = 0;
}