简体   繁体   English

在Java中使用正则表达式验证电子邮件

[英]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): 这是我的正则表达式(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.uk
    • www.fightclub.lk www.fightclub.lk
    • www.fightclub.sa www.fightclub.sa
    • www.fightclub.cc www.fightclub.cc
    • www.fightclub.jp www.fightclub.jp
    • www.fightclub.se www.fightclub.se
    • www.fightclub.xy www.fightclub.xy
    • www.fightclub.gi www.fightclub.gi
    • www.fightclub.rl www.fightclub.rl
    • www.fightclub.ss www.fightclub.ss
  • username has 3 to 6 characters(only lowercase English letters and numbers) 用户名包含3到6个字符(仅小写英文字母和数字)

examples: 例子:

sonia6@fightclub.com is valid sonia6@fightclub.com valid

am@fightclub2.lk is invalid am@fightclub2.lk invalid

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 [a-z0-9]{3,6}小写字母或数字,长度为3-6个字符
  3. followed by @fightclub 其次是@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 演示: https : //regex101.com/r/rYYXYA/1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM