Reputation: 83
I have question using rtb and regex together.
I have a text like vK-lKs-812349;jra_6993788428;Enx-2674@62
My function will split the data by deli'ter ';' and matches each part with regex patterns
"^[a-zA-Z]{2}-[a-zA-Z]{3}-[0-9]{7}$"
"^[a-zA-Z]{3}_[0-9]{10}$"
"^[a-zA-Z]{3}-[0-9&~%@*^<>!#$]{7}$"
What I want is I have this data in line in rtb (all line have a similar data i.e, same pattern of data). When I Click a button function should get the line in which the cursor is and it should match the pattern and if regex doesn't match it should change the color of that particular part of text.
Example: If I'm in line 2 and the 2nd part data is 'zza 6993788428' then as there is _ missing regex will not be match so I want that this part in the rtb should get the color red.
Your help will be appreciated. Thanks...
Upvotes: 1
Views: 268
Reputation: 83
I found code for this question by myself. I created a timer that gets the line no from the rtb and puts in a global variable.
timer1.Start();
timer1.Tick += new EventHandler(LineEvent);
and the global variable and events are
public static class foo
{
public static int lineno;
}
private void LineEvent(object sender, EventArgs eArgs)
{
int IndexCoun = rtb1.SelectionStart;//Index count where actually the mouse is clicked in the richtextbox
foo.lineno = rtb1.GetLineFromCharIndex(IndexCoun);//Get the line no
}
And the main function that matches the function and changes the rtb is
private void RegexCla(string value, string pattern, string data)
{
if(Regex.IsMatch(value, pattern) == true)
{
int index = 0;
for (int i = 0; i < foo.lineno; i++)
{
int gelen = rtb1.Lines[i].Length;
index = index + gelen;
}
rtb1.Find(data, index, RichTextBoxFinds.WholeWord);
rtb1.SelectionColor = Color.Green;
rtb1.SelectedText = value;
}
else
{
int index = 0;
for (int i = 0; i < foo.lineno; i++)
{
int gelen = rtb1.Lines[i].Length;
index = index + gelen;
}
rtb1.Find(data, index, RichTextBoxFinds.WholeWord);
rtb1.SelectionColor = Color.Red;
}
}
And finally the button click is
private void RegBtn_Click(object sender, EventArgs e)
{
string[] rtblines = rtb1.Lines;
var reqtext = rtblines[foo.lineno];
string[] reclis = reqtext.Split(';');
RegexCla(reclis[0].Replace(" ", ""), "^[a-zA-Z]{2}-[a-zA-Z]{3}-[0-9]{7}$", reclis[0]);
}
Thanks for all your support @King.
Upvotes: 1
Reputation: 179
private void Findword(string FindText, RichTextBox rt)
{
try
{
List<int> lst = new List<int>();
bool IsRun = true;
int Index = 0;
int count =0;
lst.Add(-1);
lst.Add(0);
lst.Add(rt.TextLength);
while (IsRun)
{
Index = rt.Find(FindText, Index, RichTextBoxFinds.WholeWord);
if (lst.Contains(Index))
break;
else
count++;
lst.Add(Index);
Index += FindText.Length;
}
MessageBox.Show(FindText + " Count: " + count);
}
catch { }
}
Upvotes: 1
Reputation: 179
Try this
if (!string.IsNullOrEmpty(richTextBox1.Text))
{
int lineStart = 0;
foreach (string item in richTextBox1.Lines)
{
List<string> lst = item.Split(new char[] { ';' }).ToList();
if (lst.Count == 3)
{
if (!Regex.IsMatch(lst[1], "^[a-zA-Z]{3}_[0-9]{10}$"))
{
int Start = richTextBox1.Find(item, lineStart, richTextBox1.TextLength, RichTextBoxFinds.WholeWord);
int End = Start + item.Length;
richTextBox1.Select(Start, End);
richTextBox1.SelectionBackColor = Color.Red;
}
else
{
int Start = richTextBox1.Find(item, lineStart, richTextBox1.TextLength, RichTextBoxFinds.WholeWord);
int End = Start + item.Length;
richTextBox1.Select(Start, End);
richTextBox1.SelectionBackColor = Color.Green;
}
}
lineStart += item.Length;
}
}
Upvotes: 1