views:

321

answers:

1

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:

  1. 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);
    }
    
  2. 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;
    }
    
  3. 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
SWEET! Thanks! By the way, is thanking people in comments within etiquette here, or is it considered unnecessary noise?
JCCyC
I think comments are fine :) Glad I could help
Jeff Moser
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
Problem: Regex.Escape() escapes spaces; I want "Internet Explorer" to be considered "not a pattern." :(
JCCyC
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