Does the System.Text.RegularExpressions namespace offer me anything to discover whether an input string has ("abc[0-9]" or "^aeiou$") or has not ("abc123") metacharacters? Or do I have to check manually for non-escaped characters in a certain list?
+4
A:
You have at least three options:
Use Regex.Escape and compare the result:
private static bool ContainsMetaCharacters(string s) { if (String.IsNullOrEmpty(s)) { return false; } string escaped = Regex.Escape(s); return !escaped.Equals(s, StringComparison.Ordinal); }
Apply Regex.Escape to each character to see if the escaped value is different:
private static bool ContainsMetaCharacters(string s) { if (String.IsNullOrEmpty(s)) { return false; } for (int i = 0; i < s.Length; i++) { if (Regex.Escape(s.Substring(i,1))[0] != s[i]) { return true; } } return false; }
Create your own based on the fact that the metachars shouldn't change:
private static readonly char[] _MetaChars = new char[] { '\t', '\n', '\f', '\r', ' ', '#', '$', '(', ')', '*', '+', '.', '?', '[', '\\', '^', '{', '|' }; private static bool ContainsMetaCharacters(string s) { if(String.IsNullOrEmpty(s)) { return false; } return s.IndexOfAny(_MetaChars) >= 0; }
The third approach offers more control depending on the RegexOptions you use in your Regex. For example, if you'll never use RegexOptions.IgnorePatternWhitespace, you can remove space as a metachar.
Jeff Moser
2009-06-18 16:42:52
SWEET! Thanks! By the way, is thanking people in comments within etiquette here, or is it considered unnecessary noise?
JCCyC
2009-06-18 16:44:48
I think comments are fine :) Glad I could help
Jeff Moser
2009-06-18 16:45:24
I came across this method when I did a deep dive on .net's regular expression engine: http://www.moserware.com/2009/03/how-net-regular-expressions-really-work.html
Jeff Moser
2009-06-18 16:47:19
Problem: Regex.Escape() escapes spaces; I want "Internet Explorer" to be considered "not a pattern." :(
JCCyC
2009-06-19 16:25:55
This is subtle thing. Consider:string hw = "Hello World";bool hw1 = Regex.IsMatch(hw, "Hello World");bool hw2 = Regex.IsMatch(hw, "Hello World", RegexOptions.IgnorePatternWhitespace);bool hw3 = Regex.IsMatch(hw, @"\w+ \w+");bool hw4 = Regex.IsMatch(hw, @"\w+ \w+");bool hw5 = Regex.IsMatch(hw, @"\w+ \w+", RegexOptions.IgnorePatternWhitespace);hw1 = true, hw2 = false, hw3=true, hw4=false, hw5=true. Note how in the hw2 context it's false because the Regex tree is looking for a literal. In the hw5 sense it's true because the space is treated as a whitespace metachar.
Jeff Moser
2009-06-20 13:15:28