【问题标题】:Regex to pick the alias from email address正则表达式从电子邮件地址中选择别名
【发布时间】:2021-07-26 15:21:03
【问题描述】:

我需要识别给定单元格中的所有电子邮件地址,该单元格用任何特殊字符括起来,写成任意数量的多行。

这是我构建的。

"(!\s<,;-)[a-zA-Z0-9]*@"

有什么改善吗?

【问题讨论】:

  • 你是这个意思吗? [!\s&lt;,;-][a-zA-Z0-9]*@[^\s@]+regex101.com/r/KjZYeo/1
  • 请提供一些示例输入以及您要从中匹配的确切内容。
  • 示例:来自 xyz123@gmail.com 我需要值 xyz123
  • 好的,([A-Za-z0-9]+)@ 对你来说还不够好吗?
  • 但是xyz123@gmail.com 没有包含在任何特殊字符中,对吧?仅获得匹配[a-zA-Z0-9]+(?=@)

标签: regex pattern-matching regex-lookarounds non-greedy


【解决方案1】:

模式(!\s&lt;,;-)[a-zA-Z0-9]*@ 从字面上捕获!\s&lt;,;- 开始。如果要匹配列出的字符中的 1 个,可以改用字符类 [!\s&lt;,;-]

如果你想在 xyz123@gmail.com 中匹配 xyz123,你可以使用:

[a-zA-Z0-9]+(?=@)

模式匹配

  • [a-zA-Z0-9]+ 匹配任何列出的范围的 1+ 次出现
  • (?=@) 在当前位置的右侧直接断言(不匹配)@

查看正则表达式demo

【讨论】:

    【解决方案2】:

    使用

    ([a-zA-Z0-9]\w*)@
    

    regex proof

    解释

    --------------------------------------------------------------------------------
      (                        group and capture to \1:
    --------------------------------------------------------------------------------
        [a-zA-Z0-9]              any character of: 'a' to 'z', 'A' to
                                 'Z', '0' to '9'
    --------------------------------------------------------------------------------
        \w*                      word characters (a-z, A-Z, 0-9, _) (0 or
                                 more times (matching the most amount
                                 possible))
    --------------------------------------------------------------------------------
      )                        end of \1
    --------------------------------------------------------------------------------
      @                        '@'
    

    【讨论】:

      猜你喜欢
      • 2011-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-25
      • 2011-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多