Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Regular Expression for email validation in DataAnnotations - Double backslash

Saw this code for regular expression validation of an email address via data annotations.

I Can't work out the purpose of the double backslash.

To me it's saying there must be a backslash in the email - but I know that this isn't what it is doing!!!

 [RegularExpression(".+\\@.+\\..+",   ErrorMessage="Please enter a valid email")]
like image 501
AJM Avatar asked Nov 28 '25 20:11

AJM


1 Answers

The backslash is an escape character both in C# and in a regex. So, in C#, "\\" equals to a single backslash. The resulting backslash is then used to escape the ., which is a metacharacter and therefore must be escaped. I don't know why the @ is escaped however.

like image 60
Etienne de Martel Avatar answered Nov 30 '25 09:11

Etienne de Martel



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!