Home > OS >  Using regex to pull domain from URL
Using regex to pull domain from URL

Time:12-06

I'm currently working on a regex query to pull out the domain name of a URL. The catch is, I only want to pull the domain if it has the following format:

www.miami-dade.com

If the domain is www.miamidade.com, i don't want to detect that. The part I'm struggling with is, I also don't want to detect on the domain if it has more than one dash in it. For example:

www.florida-miami-dade.com shouldn't be detected.

This is my regex, anyone have any idea on what I should do? (?<=.)[a-zA-Z0-9] -[a-zA-Z0-9-_]

CodePudding user response:

This should do it:

www\.[^-] -[^-] \.com

CodePudding user response:

Try this:

^\w \.\w -\w \.\w $

See live demo.

The regex \w means "word character", which includes letters, numbers and the underscore.

If you strictly want only letters, use

^[a-z] \.[a-z] -[a-z] \.[a-z] $
  • Related