Parsing Quantifier {x,y} Following Nothing Considered Harmful

If you normally do a Regex.Escape(string) in C# to get some text ready for use as a regular expression, be aware that a string like ^Lijit (+http://www.lijit.com/)$ will be escaped like so ^Lijit\ \(+http://www\.lijit\.com/\)$ instead of ^Lijit\ \(\+http://www\.lijit\.com/\)$ as you would expect.

Using that “escaped” regular expression will result in an ArgumentException along the lines of parsing "^Lijit (+http://www.lijit.com/)$" - Quantifier {x,y} following nothing. That message is virtually un-Google-able until now.

The .NET exception occurs, I think, because the parser just doesn’t understand the unescaped quantifier being next to an escaped control character. I believe that the escaping misses it because it uses that same parser because the plus sign is definitely one of the characters that the Escape method can handle.

Advertisements

%d bloggers like this: