Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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
good
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.
  Permalink  
Comments
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 Schatak 394
1 OriginalGriff 300
2 Abhijit Ghosh (Subho) 125
3 Andreas Gieriet 120
4 _Amy 115
0 OriginalGriff 7,137
1 Sergey Alexandrovich Kryukov 5,628
2 Maciej Los 3,529
3 Peter Leow 3,373
4 DamithSL 2,505


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