【问题标题】:find emails in a String [duplicate]在字符串中查找电子邮件[重复]
【发布时间】:2013-03-20 04:21:38
【问题描述】:

我正在尝试从类似以下的字符串中获取电子邮件:

"*** test@gmail.com&&^ test2@gmail.com((& ";

private static Pattern p = Pattern.compile("(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$)");

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

我怎样才能得到所有?

【问题讨论】:

标签: java regex


【解决方案1】:

试试

    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());
    }

【讨论】:

  • 对我不起作用
  • 这个链接的解决方案很完美,handyopinion.com/…
  • 这是一个使用答案的 Kotlin 函数:@JvmStatic fun extractEmails(text: 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()) } 返回列表 }
【解决方案2】:

只需删除 ^$ 锚点即可。

【讨论】:

    猜你喜欢
    • 2015-04-18
    • 1970-01-01
    • 2023-04-02
    • 2014-05-09
    • 1970-01-01
    • 2015-01-04
    • 2020-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多