Reputation: 7619
I'm writing a simple regex in c# to locate backslashes not preceded or followed by any backslashes:
Regex reg = new Regex(".*(?<!\\)\\(?!\\).*");
However, this statment generates an ArgumentException: "parsing ".(?" - Not enough )'s"
The group parentheses seem to match. Can anyone spot the problem?
Upvotes: 2
Views: 7405
Reputation: 2056
use the string literal @
Regex reg = new Regex(@".*(?<!\\)\\(?!\\).*");
Upvotes: 4
Reputation: 22717
Put the @ symbol in front of your string, otherwise you need to double-escape the slashes (once for C#, and once for Regex).
Regex reg = new Regex(@".*(?<!\\)\\(?!\\).*");
or
Regex reg = new Regex(".*(?<!\\\\)\\\\(?!\\\\).*");
Upvotes: 16