0

I have a regex that I use to validate emails and in JavaScript it looks like this:

function CheckIfValidEmail(TheEmail) {

    return /^([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22))*\x40([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d))*$/.test(TheEmail);
}

I didn't create that regex, I found it online. I want to use the same regex in my C# code with an extension method like this:

public static bool IsValidEmailAddress(this string TentativeEmailAddress)
{
    string Pattern = "@/^([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22))*\x40([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d))*$/";
    Regex regex = new Regex(Pattern);
    
    return regex.IsMatch(TentativeEmailAddress.ToLower());
}

Basically, I copy-paste the regex in the C# code expecting it to work but instead I get an error message that says parsing 'the regex' - Too many )'s.

What do I need to change to make this work?

4
  • holy sweet lord. let me suggest you try something like this: composableregex.apphb.com to help you break this kind of thing up. full disclosure, it's a site i wrote to build composable regex and help manage that. Commented Feb 21, 2014 at 0:31
  • 2
    I know I'll need a new pair of glasses, that regex broke mine ;-) Commented Feb 21, 2014 at 0:32
  • @devshorts: is your site still available? That link doesn't work for me. Commented Mar 28, 2023 at 10:36
  • 1
    @halfer its not but the github is github.com/devshorts/ComposableRegex Commented Apr 7, 2023 at 21:40

1 Answer 1

7

I think you misplaced @ which should be before " in C# so that you don't have to double backslashes. Also, you don't need to put regex in / ... /

string Pattern = @"^ ... $";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.