0

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:

  • Mail domain is from the list:
    • www.fightclub.uk
    • www.fightclub.lk
    • www.fightclub.sa
    • www.fightclub.cc
    • www.fightclub.jp
    • www.fightclub.se
    • www.fightclub.xy
    • www.fightclub.gi
    • www.fightclub.rl
    • www.fightclub.ss
  • username has 3 to 6 characters(only lowercase English letters and numbers)

examples:

sonia6@fightclub.com is valid

am@fightclub2.lk is invalid

Sebastian Lenartowicz
  • 4,695
  • 4
  • 28
  • 39
Intern
  • 19
  • 9
  • *..username has 3 to 6 characters(only lowercase English letters and numbers)..* -> `[a-z0-9]{6}` will do it for you. Rest you didn't provide the information for other half. – Am_I_Helpful Jul 22 '17 at 19:12
  • See [*Java regex email*](https://stackoverflow.com/questions/8204680/java-regex-email). – Wiktor Stribiżew Jul 22 '17 at 19:12
  • @WiktorStribiżew i tried with it also.but i faild. can u show me correct code.i am new for this – Intern Jul 22 '17 at 19:14

1 Answers1

2

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

Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
  • i got `valid` to `am@fightclub2.lk` also.can u help me – Intern Jul 22 '17 at 19:23
  • @Intern I missed initial `^` and last `$`. Check regex again now. Also check the demo. – Raman Sahasi Jul 22 '17 at 19:25
  • no friend.i got `am@fightclub2.lk` is `valid`.but should be invalid – Intern Jul 22 '17 at 19:29
  • question (too lazy to check): does regex101 support Java's idiom of regular expressions? – user85421 Jul 22 '17 at 19:29
  • @Intern I've created this online compilation of code with exacts same case. https://ideone.com/2X47NH . Its returning false. – Raman Sahasi Jul 22 '17 at 19:34
  • i tried with this code `boolean x=arr[j].matches("^[a-z0-9]{3,6}@fightclub\\.(?:uk|lk|sa|cc|jp|se|xy|gi|rl|ss)$"); if (x == false) { System.out.println("INVALID"); //return false; } else { System.out.println("VALID"); //return true; }` – Intern Jul 22 '17 at 19:36
  • 1
    @CarlosHeuberger I haven't encountered any situation until now where it won't work with java. However, you're right, this flavour isn't mentioned officially on their website. – Raman Sahasi Jul 22 '17 at 19:36
  • @Intern I'm not sure what's there in `arr[j]`. Please try it once after hardcoding the value like: `boolean x= "am@fightclub2.lk".matches("^[a-z0-9]{3,6}@fightclub\\.(?:uk|lk|sa|cc|‌​jp|se|xy|gi|rl|ss)$"‌​);` – Raman Sahasi Jul 22 '17 at 19:38
  • @Intern or including the tested address in the message - the expression seems prefect for me. And tested on [RegEx Planet](http://fiddle.re/nzx1yd) – user85421 Jul 22 '17 at 19:40
  • you are correct mate.false is mine.so sorry and you are really great man.thank you very much – Intern Jul 22 '17 at 19:40