如何使用Pattern.compile检查字符串是否包含"@".像这样的东西 - >>
Pattern pattern = Pattern.compile("^[\\w\\.-]*@[\\.\\w-]*$");
Matcher matcher = pattern.matcher(string);
return matcher.matches();
Run Code Online (Sandbox Code Playgroud)
如果你只需要检查它是否包含@,那么只需要一个"@"的正则表达式:
Pattern pattern = Pattern.compile("@");
Matcher matcher = pattern.matcher(string);
return matcher.find();
Run Code Online (Sandbox Code Playgroud)
(请注意,matches将查看整个输入是否与模式匹配,而find只是查看模式是否存在于输入中,这是您需要的.)
有什么理由不使用x.indexOf('@') != -1或x.contains("@")尽管?
| 归档时间: |
|
| 查看次数: |
107 次 |
| 最近记录: |