You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用正则表达式匹配链接且排除邮箱地址?

修改正则表达式排除邮箱匹配,保留合法链接

你当前使用的正则\w+\.(?:com|net|org)(?:\W|$)(实际会替换为全部约1500种顶级域名)可以匹配合法链接,但会误匹配邮箱地址中的域名部分(比如name@example.com里的example.com)。

解决方案

在原有正则前添加负向后瞻断言,修改后的正则为:

(?<!@)\w+\.(?:com|net|org)(?:\W|$)

原理说明

  • (?<!@) 是负向后瞻断言,它会检查当前匹配位置的前一个字符不是@,从而排除邮箱地址中@之后的域名部分。
  • 原有正则的逻辑保持不变,依然能匹配https://www.example.com、www.example.net、example.org这类合法链接,同时不会误匹配name@example.com中的域名。

测试验证

  • 可匹配的合法链接示例:
    • there is a valid link https://www.example.com 中的example.com
    • there is a valid link www.example.net 中的www.example.net
    • there is a valid link example.org 中的example.org
  • 不会匹配的邮箱示例:
    • there is no valid link name@example.com 中的example.com不会被识别

内容的提问来源于stack exchange,提问作者dafie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 12:32:31