0

I am looking to match all email addresses from a specific domain.

Any email coming from example.com or foo.example.com should match, everything else should be rejected. To do this, I could do some basic string matching to check if the given string ends with, or contains, example.com which would work fine but it also means that something like fooexample.com will pass.

Hence, based on the above requirements, I started working on a pattern that would pass the domain and its sub-domain. I was able to come up with the following regex pattern:

`/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.example.com\b/i`

This only matched subdomains, but I have seen the pattern at "How to match all email addresses at a specific domain using regex?" which handles the main domain.

Is there a way to combine these two into something that works for any address from example.com.

4

3 に答える 3

2

どうですか

/\b(?:(?![_.-])(?!.*[_.-]{2})[a-z0-9_.-]+(?<![_.-]))@(?:(?!-)(?!.*--)[a-z0-9-]+(?<!-)\.)*example\.com\b/i
于 2012-09-13T23:07:29.607 に答える
0

これは、a +b@example.comやa+b@i.example.comのような「tagged」および「tagged-subdomain」メールにも一致します。

(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@(?:(?!-)(?!.*--)[a-z0-9-]+(?<!-)\.)*example\.com\b

お役に立てば幸いです

于 2012-09-14T00:00:19.327 に答える
0

「複雑な正規表現で電子メールアドレスを検証するのをやめる」を読むことをお勧めします。

その時点から、私は次のことを探します。

/@.*\bexample\.com/

例えば:

%w[foo@example.com foo@barexample.com foo@subdomain.example.com].grep(/@.*\bexample\.com/)
=> ["foo@example.com", "foo@subdomain.example.com"]

メンテナンスの悪夢である正規表現に行き着くのはあまりにも簡単であり、それでは必要なことが達成されません。シンプルにすることを強くお勧めします。

于 2012-09-14T01:53:46.173 に答える