Search code examples
rubyregexrubular

Looking for a way to match any domain email ending with certain tld


New to regex and ruby was looking for a way to match any domain ending with certain tld

I have the following emails:

[email protected]
[email protected]
[email protected]
[email protected]

I am trying to write a regular expression that will match any email with the top level domain .mil and .gov, but not the rest. I've tried the following:

/(..).mil/

But I don't know how to get it to match everything before that .mil

I'm using ruby. Here's what I was trying in rubular: http://rubular.com/r/BP7tqgAntY


Solution

  • Think you mean this,

    ^(.*)\.(?:gov|mil)$
    

    In ruby,

    string.scan(/^.*(?=\.(?:gov|mil)$)/)
    

    DEMO