验证Java中包含非英语(UTF-8)字符的电子邮件地址

发布于 2024-12-06 16:08:38 字数 285 浏览 0 评论 0原文

我有以下电子邮件 ID,

闪闪发光@闪闪发光.com 

我需要在服务器端验证此类电子邮件,以便用户无法输入此类电子邮件..
我已经通过使用下面的正则表达式解决了 javascript 中的类似问题 -

/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/gi

但是。无法在java中做同样的事情。请帮助我。
提前致谢!!!

i am having fallowing email id

闪闪发光@闪闪发光.com 

i need to validate this type of email at server side so that user can not enter this type of email..
i have solved similar problem in javascript by using below regex-

/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/gi

But. unable to do same thing in java.Please help me guys.
Thanks in advance!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ヅ她的身影、若隐若现 2024-12-13 16:08:38

Java 正则表达式模式 (?i)[-a-z0-9+_][-a-z0-9+_.]*@[-a-z0-9][-a-z0-9 .]*\\.[az]{2,6} 应该足够了。该模式的含义如下:

(?i)            # Case insensitive flag
[-a-z0-9+_]     # First character
[-a-z0-9+_.]*   # Zero or more characters
@               # Literal '@' character
[-a-z0-9]       # Match a single character
[-a-z0-9.]*     # Match zero or more characters
\.              # Literal '.' character
[a-z]{2,6}      # Match 2 through 6 alpha characters

以下测试代码......

final String ps =
        "(?i)[-a-z0-9+_][-a-z0-9+_.]*@[-a-z0-9][-a-z0-9.]*\\.[a-z]{2,6}";
final Pattern p = Pattern.compile(ps);
for (String s : new String[] {"[email protected]", "[email protected]",
        "[email protected]", "[email protected]", "[email protected]", "[email protected]",
        "[email protected]", "[email protected]", "[email protected]", "[email protected]",
        "闪闪发光@闪闪发光.com"})
{
    final Matcher m = p.matcher(s);
    if (m.matches()) {
        System.out.println("Success: " + s);
    } else {
        System.out.println("Fail: " + s);
    }
}

将输出:

Success: [email protected]
Success: [email protected]
Success: [email protected]
Success: [email protected]
Success: [email protected]
Success: [email protected]
Success: [email protected]
Success: [email protected]
Fail: [email protected]
Fail: [email protected]
Fail: 闪闪发光@闪闪发光.com

通过使用 Matcher.matches() 方法,您不需要包含 ^ 行首或 $ 行尾边界匹配构造,因为 Matcher.matches() 将匹配整个字符串。

The Java regular expression pattern (?i)[-a-z0-9+_][-a-z0-9+_.]*@[-a-z0-9][-a-z0-9.]*\\.[a-z]{2,6} should suffice. Here's what the pattern means:

(?i)            # Case insensitive flag
[-a-z0-9+_]     # First character
[-a-z0-9+_.]*   # Zero or more characters
@               # Literal '@' character
[-a-z0-9]       # Match a single character
[-a-z0-9.]*     # Match zero or more characters
\.              # Literal '.' character
[a-z]{2,6}      # Match 2 through 6 alpha characters

The following test code ...

final String ps =
        "(?i)[-a-z0-9+_][-a-z0-9+_.]*@[-a-z0-9][-a-z0-9.]*\\.[a-z]{2,6}";
final Pattern p = Pattern.compile(ps);
for (String s : new String[] {"[email protected]", "[email protected]",
        "[email protected]", "[email protected]", "[email protected]", "[email protected]",
        "[email protected]", "[email protected]", "[email protected]", "[email protected]",
        "闪闪发光@闪闪发光.com"})
{
    final Matcher m = p.matcher(s);
    if (m.matches()) {
        System.out.println("Success: " + s);
    } else {
        System.out.println("Fail: " + s);
    }
}

... will output:

Success: [email protected]
Success: [email protected]
Success: [email protected]
Success: [email protected]
Success: [email protected]
Success: [email protected]
Success: [email protected]
Success: [email protected]
Fail: [email protected]
Fail: [email protected]
Fail: 闪闪发光@闪闪发光.com

By using the Matcher.matches() method, you don't need to include the ^ start-of-line or $ end-of-line boundary matching constructs since Matcher.matches() will match on the whole string.

神回复 2024-12-13 16:08:38

[更新] 抱歉js代码。试试这个:

import java.util.regex.Matcher;
import java.util.regex.Pattern;


    public class EmailValidator{

          private Pattern pattern;
          private Matcher matcher;

          private static final String EMAIL_PATTERN = 
                       "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@
                       [A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

          public EmailValidator(){
              pattern = Pattern.compile(EMAIL_PATTERN);
          }

          /**
           * Validate hex with regular expression
           * @param hex hex for validation
           * @return true valid hex, false invalid hex
           */
          public boolean validate(final String hex){

              matcher = pattern.matcher(hex);
              return matcher.matches();

          }
    }

[Update] Sorry for the js code. Try this:

import java.util.regex.Matcher;
import java.util.regex.Pattern;


    public class EmailValidator{

          private Pattern pattern;
          private Matcher matcher;

          private static final String EMAIL_PATTERN = 
                       "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@
                       [A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

          public EmailValidator(){
              pattern = Pattern.compile(EMAIL_PATTERN);
          }

          /**
           * Validate hex with regular expression
           * @param hex hex for validation
           * @return true valid hex, false invalid hex
           */
          public boolean validate(final String hex){

              matcher = pattern.matcher(hex);
              return matcher.matches();

          }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文