Make search terms on richtextbox case insensitive c# - c#

I have a richtextbox that I've added a search and highlight function to but it will only search for exactly what the user types. I know this is because of the MatchCase property but none of the other options seem to do the job. Here is my code:
private void btnSourceSearch_Click(object sender, EventArgs e)
{
int start = 0;
int end = richTextBox1.Text.LastIndexOf(textBox1.Text);
richTextBox1.SelectAll();
richTextBox1.SelectionBackColor = Color.White;
while(start < end)
{
richTextBox1.Find(textBox1.Text, start, richTextBox1.TextLength, RichTextBoxFinds.MatchCase);
richTextBox1.SelectionBackColor = Color.Yellow;
start = richTextBox1.Text.IndexOf(textBox1.Text, start) + 1;
}
}
Any help would be greatly appreciated. It's probably simple but I've been looking at code for a good few hours over the last week and it's beginning to look a lot like the Matrix!
Thanks

I don’t know if you are familiar with regular expressions, but they are useful in this situation. I am not that familiar with them but felt I would give this a shot using them. Without them, using your approach, you will have to check somehow all the case possibilities. That’s where regular expressions are your friend. Below is the code that creates a regular expression from the text in the text box. Then I use that expression to get the Matches in the text in the RichTexBox to highlight. Hope this helps.
private void button1_Click(object sender, EventArgs e) {
richTextBox1.SelectAll();
richTextBox1.SelectionBackColor = Color.White;
if (textBox1.Text.Length < 1)
return;
string pattern = #"\b" + textBox1.Text.Trim() + #"\b";
Regex findString = new Regex(pattern, RegexOptions.IgnoreCase);
foreach (Match m in findString.Matches(richTextBox1.Text)) {
richTextBox1.Select(m.Index, m.Length);
richTextBox1.SelectionBackColor = Color.Yellow;
}
}

You could do your search by adding Text.ToUpper() method.
Add .ToUpper() method in your richTextBox1.Text and search text both.

As mentioned use the ToUpper() or ToLower() method for all texts that you operate with. But I also wonder if you shouldn't add the event to search while typing instead of waiting for full string. That would be more intuitive and easier to troubleshoot at all.

Related

c# replace string with many "x" [duplicate]

This question already has answers here:
Best way to repeat a character in C#
(21 answers)
Closed 1 year ago.
I am writing a windows form app in C#. I have a label with a random word, e.g. "computer". User has to guess what is this word by guessing letter by letter. But word "computer" must be replaced with as many "x" as there is letters in a word, so in this example user should see "xxxxxxxx".
I tried to replace it with regex like this:
private void Form1_Load(object sender, EventArgs e)
{
word.Text = Regex.Replace(word.Text, #"^[a-zA-Z]+$", "x");
}
But it only replace with single "x".
I also tried with for loop, but the result is the same:
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i <= word.Text.Length; i++)
{
word.Text = Regex.Replace(word.Text, #"^[a-zA-Z]+$", "x");
}
}
You can create a new string of "x" repeated the number of characters in your word. No need for regexes.
word.Text = new string('x', word.Text.Length);
Approach without RegEx
string wordText = "computer";
wordText = string.Concat(wordText.Select(x => char.IsLetter(x) ? 'x' : x));
if you want to stay with RegEx, replace #"^[a-zA-Z]+$" with "[a-zA-Z]" to match every single character
Try this:
word.Text=String.Join("",word.Text.Select(s=>s='x').ToArray());

Replace to Colored Text

So I want to use the Text.Replace function to replace non-colored text with colored text, but that non-colored text has to be a specific word.
eg.
private void timer1_Tick(object sender, EventArgs e)
{
int index = 0;
while(index < richTextBox1.Text.LastIndexOf(textBox1.Text))
{
richTextBox1.Find("if");
richTextBox1.Text.Replace("if", Text.Color.DarkGreen "if");
}
}
}
The coloring is not so hard but defining the term 'word' is not so simple..
Here is an example for both:
I assume that you can live with the Regex definition of word boundaries. You may need to improve on the wordPattern depending on your needs, i.e. on your grammar! (See here for an example!)
int ColorWord(RichTextBox rtb, string word, Color foreColor, Color backColor)
{
string wordPattern = #"\b" + word + #"\b";
var matches = Regex.Matches(rtb.Text, wordPattern);
for (int i = matches.Count - 1; i >= 0; i--)
{
var m = matches[i];
rtb.SelectionStart = m.Index;
rtb.SelectionLength = m.Length;
rtb.SelectionColor = foreColor;
rtb.SelectionBackColor = backColor;
}
return matches.Count;
}
This function will set fore- and backcolor for each occurance of a given word in a RichTextBox. It transverses the matches from the back just for good measure as we don't really modify any text, so the letters won't shift; so we could have looped from start to end as well; but maybe you want to adapt it one day for changing words..
Once you understand how coloring (or formatting in any other way) the RTB text works, i.e. by first selecting a portion of the text and then changing the RTB.SelectedXXX property, it should be simple to modify..
Note that the word boundaries are meant to work with normal text. It is up to you to define maybe new rules to include or exclude characters for the language you want to highlight..

String checking with Regex

I have a textBox in my program that contains a string that has to meet a few requirements. I'm asking this question to figure out the best way to meet these requirements.
This string can't be NullOrEmpty and it must be completely composed of integers. The string can also contain spaces, which is my sticking point because spaces aren't integers.
This is what I'm working with (I'm aware that it may be a bit redundant at the moment):
//I test the string whenever the textBox loses focus
private void messageBox_LostFocus(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(TextBox.Text))
ButtonEnabled = true;
else if (Regex.IsMatch(TextBox.Text, #"^\d+$") == false)
{
//I think my problem is here, the second part of the if statement doesn't
//really seem to work because it accepts characters if there is a space
//in the string.
if (TextBox.Text.Contains(" ") && !Regex.IsMatch(TextBox.Text, #"^\d+$"))
ButtonEnabled = true;
else
{
MessageBox.Show("Illegal character in list.", "Warning!", MessageBoxButton.OK, MessageBoxImage.Warning);
ButtonEnabled = false;
}
}
else
ButtonEnabled = true;
}
I got the Regex solution from this answer.
Question: How do I make it so that this textBox only accepts values like these:
"345 78" or "456"?
The regular expression seems simple enough. It could be something along the line of (with the specified constraints):
^([\s\d]+)?$
In your LostFocus handler, you could use something like this:
ButtonEnabled = Regex.IsMatch(TextBox.Text, #"^([\s\d]+)?$");
The button will be enabled if:
It's an empty string
It contains only digits and spaces
If you want a regular expression that will extract the numbers as well, you could change the pattern to:
^(\s*(?<number>\d+)\s*)*$
And use the number capture group.
Note that the first pattern will match strings that are composed of spaces only.

Simple problem with replacing string value C#

i'm in a bit of a bind with a problem that's supposed to mediocre, but seemingly i can't implement the solution.
I have buttons, with a single char on each of them, 26 to be exact (english alphabet),
When i click any of them, the loop iterates through the string for the text value on the buttons and replaces it with quotation marks.
The code works, and it prints out the newAlphabet without the clicked character. But when i click another button, it returns the newAlphabet albeit with the previously removed character and removes the new clicked character.
The code is as follows
static string alphabet = "abcdefghijklmnopqrstuvwxyz";
static string newAlphabet = string.Empty;
Button tempBtn = (Button)sender;
for (int i = 0; i < alphabet.Length; i++)
{
if (alphabet[i].ToString().Contains(tempBtn.Text))
{
newAlphabet = alphabet.Replace(tempBtn.Text, "");
MessageBox.Show(newAlphabet);
}
}
Sorry for grammar or spelling errors, english is not my first language.
Regards, HC
This line
newAlphabet = alphabet.Replace(tempBtn.Text, "");
means you're always getting back to "abcdefghijklmnopqrstuvwxyz" and replacing that.
If you want to keep replacing letters, you need to replace on newAlphabet.
A simpler solution would be:
static string alphabet = "abcdefghijklmnopqrstuvwxyz";
private void button1_Click(object sender, EventArgs e)
{
var tempBtn = (Button)sender;
alphabet = alphabet.Replace(tempBtn.Text, "");
MessageBox.Show(alphabet);
}
Note 1:
If the code you posted is in your button click event method then it would not compile. In C# you cannot declare variables static inside methods.
Note 2:
Strings are immutable so alphabet.Replace() returns a new string without affecting the original.
If the goal is to remove the clicked letter from the list:
static string newAlphabet = "abcdefghijklmnopqrstuvwxyz";
Button tempBtn = (Button)sender;
newAlphabet = newAlphabet.Replace(tempBtn.Text, "");
MessageBox.Show(newAlphabet);
Please note that strings are immutable in C#. "newAlphabet" is being continuously replaced by a modified "alphabet". It will never stick.

How to use C# regular expressions to emulate forum tags

I am building a forum and I want to be able to use simple square bracket tags to allow users to format text. I am currently accomplishing this by parsing the string and looking for the tags. It's very tedious, especially when I run into a tag like this [url=http://www.something.com]Some text[/url]. Having to parse the attribute, and the value, and make sure it has proper opening and closing tags is kind of a pain and seems silly. I know how powerful regular expressions are but I'm not good at them and they frustrate me to no end.
Any of you regex gurus willing to help me out? I think an example would get me started. Just a regex for finding tags like [b]bolded text[/b] and tags with attributes like the link one I listed above would be helpful. Thanks in advance!
Edit: Links to laymen's terms tutorials for regex are also helpful.
This should work. The "=something.com" is optional and accommodates single or double quotes and it also makes sure that the closing tag matches the opening tag.
protected void Page_Load(object sender, EventArgs e)
{
string input = #"My link: [url='http://www.something.com'][b]Some text[/b][/url] is awesome. Jazz hands activate!!";
string result = Parse(input);
}
//Result: My link: <b>Some text</b> is awesome. Jazz hands activate!!
private static string Parse(string input)
{
string regex = #"\[([^=]+)[=\x22']*(\S*?)['\x22]*\](.+?)\[/(\1)\]";
MatchCollection matches = new Regex(regex).Matches(input);
for (int i = 0; i < matches.Count; i++)
{
var tag = matches[i].Groups[1].Value;
var optionalValue = matches[i].Groups[2].Value;
var content = matches[i].Groups[3].Value;
if (Regex.IsMatch(content, regex))
{
content = Parse(content);
}
content = HandleTags(content, optionalValue, tag);
input = input.Replace(matches[i].Groups[0].Value, content);
}
return input;
}
private static string HandleTags(string content, string optionalValue, string tag)
{
switch (tag.ToLower())
{
case "url":
return string.Format("{1}", optionalValue, content);
case "b":
return string.Format("<b>{0}</b>", content);
default:
return string.Empty;
}
}
UPDATE
Now i'm just having fun with this. I cleaned it up a bit and replaced the " with \x22 so the entire string can easily be escaped per #Brad Christie's suggestion. Also this regex won't break if there are "[" or "]" characters in the content. Also it handles tags recursively
I'm not saying that you can't do this with regular expressions, but I think you're going to find it very, very difficult. You'll have to decide what to do with things like [b]this is [bold text[/b], and other cases where the user has [ or ] characters. And will you allow nesting? (i.e. [b]this is bold, [i]italic[/i] text[/b]).
I would suggest that you look into using something like Markdown.

Categories