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 đŸ˜‰

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s