Java 在字符串中查找电子邮件

Java 在字符串中查找电子邮件,java,regex,Java,Regex,我试图从一个字符串中获取电子邮件,该字符串类似于: “***test@gmail.com&&^ test2@gmail.com((&); 上面的代码可以收到一封电子邮件 我怎样才能得到全部呢?试试看 String s = "*** test@gmail.com&&^ test2@gmail.com((& "; Matcher m = Pattern.compile("[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.

我试图从一个字符串中获取电子邮件,该字符串类似于:

“***test@gmail.com&&^ test2@gmail.com((&);

上面的代码可以收到一封电子邮件

我怎样才能得到全部呢?

试试看

    String s = "*** test@gmail.com&&^ test2@gmail.com((& ";
    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());
    }

只需删除
^
$
锚定。

您可以在java中使用模式类[请参见此处][1][1]:请参见此处我不确定电子邮件中是否允许使用
+
字符。此链接不适用于meSolution。这是一个使用Kotlin函数的答案:@JvmStatic fun extractEmails(文本:String):MutableList{val list=mutableListOf()val m:Matcher=Pattern.compile(“[a-zA-Z0-9+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+”).Matcher(text)while(m.find(){list.add(m.group())}返回列表}
    String s = "*** test@gmail.com&&^ test2@gmail.com((& ";
    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());
    }