Error Evaluating Regular Expression
-
hi guys ! need some help.. i want to match whole words in my string using regular expression i m using Regex.IsMatch function for this and using "\b" as start and end tags for matching the word as a whole every thing is working fine but if some special character comes with the words like '***SPAM***' it throws an exception "Argument Exception , nested quantifier?" can any one help me how to deal with this as i also want these special characters to be matched. moreover there could be any special character which could occur n numver of times in the word. how to deal with that.
abhinav
-
hi guys ! need some help.. i want to match whole words in my string using regular expression i m using Regex.IsMatch function for this and using "\b" as start and end tags for matching the word as a whole every thing is working fine but if some special character comes with the words like '***SPAM***' it throws an exception "Argument Exception , nested quantifier?" can any one help me how to deal with this as i also want these special characters to be matched. moreover there could be any special character which could occur n numver of times in the word. how to deal with that.
abhinav
-
Use the Regex.Escape method to escape the characters in the string, so that you can use it in the regular expression.
--- b { font-weight: normal; }
-
thanx for the reply guffa but can u explain it with a small example if possible. thanx again
abhinav
A possibly easier solution would be to write the string (if hard-coded in the code) as:
string regex = @"regex-goes-here";
The @ symbol tells csc not to escape the C# characters like '\n', '\t' etc. (The Regex class can do this anyway when passed a literal string.)
Formula 1 - Short for "F1 Racing" - named after the standard "help" key in Windows, it's a sport where participants desperately search through software help files trying to find actual documentation. It's tedious and somewhat cruel, most matches ending in a draw as no participant is able to find anything helpful. - Shog9 Ed
-
thanx for the reply guffa but can u explain it with a small example if possible. thanx again
abhinav
Example:
// A text that contains codes that would mess up a regex
string word = @"c:\dir\body.txt";
// Create a pattern containing the text
string pattern = @"\b" + Regex.Escape(word) + @"\b";
// Create a Regex object using the pattern
Regex re = new Regex(pattern);--- b { font-weight: normal; }
-
Example:
// A text that contains codes that would mess up a regex
string word = @"c:\dir\body.txt";
// Create a pattern containing the text
string pattern = @"\b" + Regex.Escape(word) + @"\b";
// Create a Regex object using the pattern
Regex re = new Regex(pattern);--- b { font-weight: normal; }
Hi! thanks again for the reply guys. i tried the Regex.Escape method. it works good and don't throw any exception but it do not match the whole word also. m i again missing some thing.. here is my code:
public bool sSearchWholeWord() { string sSearchWord="***SPAM***"; string content="hi this is ***SPAM*** testing"; string pattern = @"\b" + Regex.Escape(sSearchWord) + @"\b"; Regex re =new Regex(pattern,RegexOptions.IgnorePatternWhitespace); try { if(re.IsMatch(content)) { return true; } else { return false; } } catch(Exception e) { Response.Write(e.ToString()); return false; } }
this returns false but according to me it should return true i dont know what m i missing
abhinav