0
private final String reg2 = "[A-Z][A-Za-z0-9_]+@[a-z]+[.com|.net][.sg|.cn|.au]?";

this line of code is supposed to be the regex for an email tester. the current email I'm using to test it is [email protected](imaginary of course). However, it seems to be always wrong. How do I change it?

3
  • "[A-Z][A-Za-z0-9_]+@[a-z]+(?:\\.com|\\.net)(?:\\.sg|\\.cn|\\.au)?" Commented Mar 26, 2015 at 16:05
  • You seem to lack a basic understanding of regex ([.com|.net]). May I suggest you start with a tutorial? Commented Mar 26, 2015 at 16:06
  • "However, it seems to be always wrong" Your problem is obvious here, but in general, you can get better answers if you describe what you expect and what it does instead. Commented Mar 26, 2015 at 16:06

1 Answer 1

3

It will be wrong because you are using character classes instead of alternative groups.

(\.com|\.net) - this is correct.

Your regex can match "[email protected]" after a small enhancement (see demo here):

[A-Z][A-Za-z0-9_]+@[a-z]+(?:\.com|\.net)(?:\.sg|\.cn|\.au)?

See more on alternative lists here and on character classes here.

Sign up to request clarification or add additional context in comments.

3 Comments

see he uses two charcater class not one. Don't combine the both.
It is just a sample to check "[email protected]". But you are right. :)
I believe that he was intending to match e.g. .com.au, since that is a common domain in Australia, not .com or .au.

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.