Problems with regular expressions
-
Hello to all, First, sorry for my english. :) I have an aplication where the user should write in a textbox a regular expression, then all the words that matches in an listBox should be selected, and it works fine, but if the user intruduces a bad regular expression (like *a instead of a*) it blows the all thing. How could i fix it. here is part of the code: //////////////////////////////////////////////////////////////////// string exp = textBox1_Tab3_Create6.Text; Regex rexp = new Regex(exp); //compara a substring de cada item com a introduzida e //selecciona se for igual for (int i = 0; i < listBox1_Tab3_Create6.Items.Count; i++) { string word = listBox1_Tab3_Create6.Items[i].ToString(); if (rexp.IsMatch(word)) listBox1_Tab3_Create6.SetSelected(i, true); } /////////////////////////////////////////////////////////////////// Thanks for any help
-
Hello to all, First, sorry for my english. :) I have an aplication where the user should write in a textbox a regular expression, then all the words that matches in an listBox should be selected, and it works fine, but if the user intruduces a bad regular expression (like *a instead of a*) it blows the all thing. How could i fix it. here is part of the code: //////////////////////////////////////////////////////////////////// string exp = textBox1_Tab3_Create6.Text; Regex rexp = new Regex(exp); //compara a substring de cada item com a introduzida e //selecciona se for igual for (int i = 0; i < listBox1_Tab3_Create6.Items.Count; i++) { string word = listBox1_Tab3_Create6.Items[i].ToString(); if (rexp.IsMatch(word)) listBox1_Tab3_Create6.SetSelected(i, true); } /////////////////////////////////////////////////////////////////// Thanks for any help
I guess by "it blows the all thing" you mean you get an exception because the regex is wrong? Nothing easier to fix:
string exp = textBox1_Tab3_Create6.Text;
try {
Regex rexp = new Regex(exp);
}
catch(ArgumentException e)
{
// something went wrong while parsing the regex, display a message box or something like that
}regards
-
Hello to all, First, sorry for my english. :) I have an aplication where the user should write in a textbox a regular expression, then all the words that matches in an listBox should be selected, and it works fine, but if the user intruduces a bad regular expression (like *a instead of a*) it blows the all thing. How could i fix it. here is part of the code: //////////////////////////////////////////////////////////////////// string exp = textBox1_Tab3_Create6.Text; Regex rexp = new Regex(exp); //compara a substring de cada item com a introduzida e //selecciona se for igual for (int i = 0; i < listBox1_Tab3_Create6.Items.Count; i++) { string word = listBox1_Tab3_Create6.Items[i].ToString(); if (rexp.IsMatch(word)) listBox1_Tab3_Create6.SetSelected(i, true); } /////////////////////////////////////////////////////////////////// Thanks for any help
Hi pcaeiro You means when u type "*a" it's show will give error? If it will give error please post that error here because of "it blows the all thing" thanks
Cheers,Earn and Enjoy RRave MCTS,MCPD http://ravesoft.blogspot.com
-
I guess by "it blows the all thing" you mean you get an exception because the regex is wrong? Nothing easier to fix:
string exp = textBox1_Tab3_Create6.Text;
try {
Regex rexp = new Regex(exp);
}
catch(ArgumentException e)
{
// something went wrong while parsing the regex, display a message box or something like that
}regards
-
Hi pcaeiro You means when u type "*a" it's show will give error? If it will give error please post that error here because of "it blows the all thing" thanks
Cheers,Earn and Enjoy RRave MCTS,MCPD http://ravesoft.blogspot.com