Oniontan
Oniontan

Reputation: 19

regex causing logic error

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?

Upvotes: 0

Views: 31

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626927

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.

Upvotes: 3

Related Questions