Intern
Intern

Reputation: 19

using regex in java for validate email

I am trying to validate a certain subset of the e-mail format with regular expressions, but what I've tried so far doesn't quite work. This is my regex (Java):

boolean x = l.matches(
    "^[_A-Za-z0-9-\\\\+]+(\\\\.[_A-Za-z0-9-]+)*@\"\n" +"+ \"[A-Za-z0-9-]+(\\\\.[A-Za-z0-9]+)*(\\\\.[A-Za-z]{2,})$"
);

Thse are the conditions that the string has to match:

examples:

[email protected] is valid

[email protected] is invalid

Upvotes: 0

Views: 1322

Answers (1)

Raman Sahasi
Raman Sahasi

Reputation: 31901

You can use:

^[a-z0-9]{3,6}@fightclub\.(?:uk|lk|sa|cc|jp|se|xy|gi|rl|ss)$
  1. ^ indicates start of string
  2. [a-z0-9]{3,6} lowercase letters or number with length 3-6 characters
  3. followed by @fightclub
  4. followed by a period \.
  5. followed by a list of domains (?: indicate that it's a non-capturing group. All your domain extensions are listed here.
  6. $ indicates end of string

DEMO: https://regex101.com/r/rYYXYA/1

Upvotes: 2

Related Questions