Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
5.00/5 (1 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

1 solution

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:
C#
string rPartial = inp.Replace("*", ".*?");
string r = rPartial.Replace("?", ".");
You can then run your regex to identify the areas you need to highlight.
 
Share this answer
 
Comments
jaipe 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900