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

正则条件非捕获组:按域名提取邮箱内容并放入Group1

正则表达式解决方案

问题分析

你当前的正则^(.*)?(?:@example1\.com)|(?=@example2\.com)$存在两个核心问题:

  • 第二个分支使用正向预查(?=@example2\.com)$,仅断言位置不匹配实际字符,根本无法捕获完整邮箱地址;
  • 第一个分支的(.*)?匹配逻辑过于宽松,且未正确锚定字符串结尾,容易出现意外匹配结果。

正确正则表达式

针对你的需求,可使用以下正则实现:

^([^@]+)@example1\.com$|^([^@]+@(?!example1\.com).+)$

逻辑说明

  • 第一个分支^([^@]+)@example1\.com$:匹配example1.com域名的邮箱,([^@]+)捕获@前的用户名到Group1;
  • 第二个分支^([^@]+@(?!example1\.com).+)$:匹配非example1.com域名的邮箱,将完整邮箱地址捕获到Group1。

测试验证

  • 输入test@example1.com → Group1结果:test
  • 输入test@example2.com → Group1结果:test@example2.com

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 16:59:53