Click here to Skip to main content
11,802,442 members (69,463 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# C#4.0
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.
jaypee1979 at 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 at 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
0 OriginalGriff 445
1 KrunalRohit 366
2 F-ES Sitecore 295
3 Afzaal Ahmad Zeeshan 230
4 CPallini 230
0 OriginalGriff 2,950
1 Maciej Los 1,910
2 KrunalRohit 1,862
3 CPallini 1,645
4 Richard MacCutchan 1,157

Advertise | Privacy | Mobile
Web01 | 2.8.151002.1 | Last Updated 18 Dec 2012
Copyright © CodeProject, 1999-2015
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