Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error parsing regex pattern

Tags:

c#

regex

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?

like image 565
Odrade Avatar asked Apr 27 '26 11:04

Odrade


1 Answers

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(".*(?<!\\\\)\\\\(?!\\\\).*");
like image 188
John Fisher Avatar answered Apr 29 '26 00:04

John Fisher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!