java 在字符串中查找电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15703704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:34:38 来源:igfitidea点击:
find emails in a String
提问by Felix
I am trying to get emails from a String which is like:
我正在尝试从字符串中获取电子邮件,如下所示:
"*** [email protected]&&^ [email protected]((& ";
"*** [email protected]&&^ [email protected]((& ";
private static Pattern p = Pattern.compile("(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)");
The code above can get one email.
上面的代码可以获得一封电子邮件。
How can I get all?
我怎样才能得到所有?
回答by Evgeniy Dorofeev
try
尝试
String s = "*** [email protected]&&^ [email protected]((& ";
Matcher m = Pattern.compile("[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+").matcher(s);
while (m.find()) {
System.out.println(m.group());
}
回答by sp00m
Just remove the ^
and $
anchors.
只需删除^
和$
锚点。