unrecognized escape sequence

While using the Regex class to validate email using C# code, an error stating “unrecognized escape sequence” was being returned when presenting the following code:

public bool ValidateEmail(string Email)
{
bool valid = Regex.Match(Email, “\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b”).Success;

return valid;
}

The solution was simple … adding an @ in front of the regex, enables the escape after each “\” in the sequence… therefore new code is:

public bool ValidateEmail(string Email)
{
bool valid = Regex.Match(Email, @”\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b”).Success;

return valid;
}

This may be valid for more other scenarios 😉