Click here to Skip to main content
13,553,630 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi, I have created a project to get the data from SQL and display in Richtextbox, I am able highlight the words in richtextbox which user had entered in textbox1.

The requirement is, few users uses wildcard like * (eg: proje*t) in the words.
Need to highlight the words in Richtextbox using Regex matching the words like
(project, projected, projection etc..).

Please help me on the same.
Posted 17-Dec-12 20:18pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

That is more complex than it looks at first glance - the regex syntax is different to the "normal" windows wildcard syntax.
However, it shouldn't be a major job to handle: jous use string.replace to change the wildcard spec to regex syntax:
string rPartial = inp.Replace("*", ".*?");
string r = rPartial.Replace("?", ".");
You can then run your regex to identify the areas you need to highlight.
jp281979 18-Dec-12 3:23am
Hi, I am not getting the grip on it, please help, below is the code which I am using for highlighting normal words.

private void HighLightRTBLISTJOBS()
string tex = txtText.Text;
if (tex.Length > 0)
int jp = -1;
int searchStart = 0;
while ((jp = rtbListJobs.Find(tex, searchStart, RichTextBoxFinds.None)) > -1)
rtbListJobs.Select(jp, tex.Length);
rtbListJobs.SelectionBackColor = Color.YellowGreen;
searchStart = jp + tex.Length;

Kindly help
OriginalGriff 18-Dec-12 4:04am
No, you can't use regex syntax in a RichTextBox.Find method call - it looks for text exactly as presented.
If you want to use a Regex that you need to do it separately.
Note that the Match class returned by the Regex includes the index at which it was found.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.180515.1 | Last Updated 18 Dec 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100