nomeacuerdo
nomeacuerdo

Reputation: 57

Regular expression to match certain subdomains

I need to match with preg_match the urls of a certain domain that end on .co but not match those who end on .com

example:

www.cool.com
www.cool.com.co [MATCH]
www.cool.co [MATCH]

I was trying to use the expression [\.co]{3} but it matches me all the URLs (www.cool.com.co)

Thanks in advance for the help ;-)

[EDIT]

URLs like www.cool.co/coolness should also be a match

Upvotes: 0

Views: 118

Answers (2)

nomeacuerdo
nomeacuerdo

Reputation: 57

Finally did it!

The regexp was: /\b\.(co)\b/g

www.cool.com
cool.com
www.cool.com.co [MATCH]
cool.com.co [MATCH]

www.cool.com/coolness
cool.com/coolness
www.cool.com.co/coolness [MATCH]
cool.com.co/coolness [MATCH]

Upvotes: 0

Siva Charan
Siva Charan

Reputation: 18064

Try this Regex

/((.*)(.co))$/gm

See this DEMO

EDIT:

/((.*)(.co)($|/)(.*))/gm

See this DEMO 2

Upvotes: 1

Related Questions