正则表达式 - 排除域 [英] Regex - exclude domain

查看:76
本文介绍了正则表达式 - 排除域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设,我有域名

@abc.com.

如果有 3 个或更多外部收件人,我需要匹配模式.

例如:致:

<块引用>

user1@abc.com;external@contoso.com;user2@abc.com;user3@abc.com;user4@abc.com;user5@abc.com;external2@contoso.com;test@google.com

我已经拥有的正则表达式如下所示:

到:(.*@[^a][^b][^c][^.][^c][^o][^m].*){3,}

它有点工作,但看起来很讨厌.也许以某种方式实现这个?^((?!@abc.com).){3,}

谢谢!对我很有帮助

解决方案

您需要匹配:

  1. A "@"
  2. 后面没有"abc."
  3. 直到下一个"@"
  4. 的更多字符
  5. (1)、(2) 和 (3) 重复 3 次

此表达式将匹配具有 3 个外部收件人的主题:

(?:@(?!abc[.]).*?){3}

(?!abc[.]) 以检查您的域后面没有跟随它..*? 用于在不同的 @ s 之间使用字符.

您也可以将 abc[.] 更改为 abc[.]com(?:[; ]|$) 或您的真实域名.

Lets say, I have domain

@abc.com.

I need to match the pattern if there are 3 or more external recipients.

For example: To:

user1@abc.com; external@contoso.com; user2@abc.com; user3@abc.com; user4@abc.com; user5@abc.com; external2@contoso.com; test@google.com

The regex I already have looks like that:

To:(.*@[^a][^b][^c][^.][^c][^o][^m].*){3,}

Its kinda working, but looking nasty. Maybe somehow implement this? ^((?!@abc.com).){3,}

Thank you! It would help me a lot

解决方案

You need to match:

  1. A "@"
  2. not followed by "abc."
  3. more characters until the next "@"
  4. (1), (2) and (3) repeated 3 times

This expression will match a subject with 3 external recipients:

(?:@(?!abc[.]).*?){3}

DEMO


It uses the negative lookahead (?!abc[.]) to check it's not followed by your domain. The .*? is there to comsume the chars between different @s.

You may as well change abc[.] to abc[.]com(?:[; ]|$) or your real domain.

这篇关于正则表达式 - 排除域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆