【问题标题】:Extract name from email using regex使用正则表达式从电子邮件中提取名称
【发布时间】:2018-03-26 14:42:15
【问题描述】:

我正在尝试从他的电子邮件中获取用户名。我知道有一些简单的方法可以实现这一点,但这让我想知道我是否可以只使用正则表达式来实现这一点。

假设用户输入以下电子邮件:user.sure_name123@mail.co

我想从那个字符串中提取:用户确定名

到目前为止我已经尝试过:

([a-zA-Z]+)

但是有了这个,域就包括在内了。使用.*(?=@),我可以得到'@'之前的所有内容。

我不知道如何将两者结合起来实现我的目标。

有什么建议吗?谢谢!

【问题讨论】:

标签: java regex


【解决方案1】:

到目前为止提出的答案似乎错过了您将两种想法组合成一个正则表达式的意图。当然,使用两个更简单。但是,它可以通过使用匹配器组并仅从我们感兴趣的组中收集数据来完成。

Java 8 版本:

public static void main(String[] args) {
    Pattern p = Pattern.compile("([a-zA-Z]+)[^a-zA-Z@]*(@.*)?");
    String input="user.sure_name123@mail.co";
    System.out.println(MatcherStream.results(p, input)
            .map(result -> result.group(1))
            .collect(Collectors.joining(" ")));
    // MatcherStream implementation http://stackoverflow.com/a/42462014/7098259
}

Java 9 版本:

在 Java 9 中流式传输匹配结果更方便。

public static void main(String[] args) {
    System.out.println(Pattern.compile("([a-zA-Z]+)[^a-zA-Z@]*(@.*)?")
            .matcher("user.sure_name123@mail.co").results()
            .map(result -> result.group(1))
            .collect(Collectors.joining(" ")));
}

替换所有版本:

最后,这不是一个纯粹的正则表达式解决方案,因为它要求您在末尾修剪掉一个额外的空格字符。但正如您所见,使用 replaceAll 更加简洁:

public static void main(String[] args) {
    String input = "user.sure_name123@mail.co";
    System.out.println(input.replaceAll("((@.*)|[^a-zA-Z])+", " ").trim());
}

输出:

用户确定的名字

【讨论】:

    【解决方案2】:

    使用以下内容:

    email.replaceAll("@.*","").replaceAll("[^a-zA-Z]+", " ").trim();
    

    这将有效地删除@ 符号之后的任何内容,然后在剩余部分中将所有非字母字符序列替换为一个空格。最后调用trim方法去除首尾空格,以防邮件地址的用户部分末尾或开头有123

    【讨论】:

    【解决方案3】:

    在 Java 中,您可以使用 Matcher 类使用正则表达式提取电子邮件的用户名部分。为了替换非字母和非数字符号,我建议您在提取一段文本后使用 String 类中的replaceAll 方法:

    java.util.regex.Pattern p = java.util.regex.Pattern.compile("^([^@]+)");
    java.util.regex.Matcher m = p.matcher("user.sure_name123@mail.co");
    
    String userName = null;
    if (m.find()) {
        userName = m.group(0).replaceAll("[^a-zA-Z]", " ");
    }
    

    【讨论】:

      【解决方案4】:

      这是我使用带有组的正则表达式模式的简单解决方案:

      private static final Pattern EMAIL = Pattern.compile("(?<one>[^\\.]+)\\.(?<two>[^_]+)_(?<three>[^@\\d]+).+");
      
      public static String getName(String email) {
          Matcher matcher = EMAIL.matcher(email);
          return matcher.matches() ? matcher.group("one") + ' ' + matcher.group("two") + ' ' + matcher.group("three") : null;
      }
      

      这是Demo at regex101.com的链接

      【讨论】:

      • 如果“名称”只有两个部分、四个部分或更多部分怎么办?
      • @PatrickParker 我只是给出了一种使用问题描述的方法。只有一种电子邮件模式。
      【解决方案5】:
      String email = "user.sure_name123@mail.co";
      String result = email.replaceAll("@.+$", ""); //user.sure_name123
      result = result.replaceAll("\\W+"," "); //user sure name123
      

      【讨论】:

      • 虽然此代码可以回答问题,但提供有关 如何为什么 解决问题的附加上下文将提高​​答案的长期价值。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-15
      • 2010-12-20
      • 1970-01-01
      • 2011-01-16
      • 1970-01-01
      • 2016-04-28
      • 2018-12-04
      相关资源
      最近更新 更多