Wouter Lemcke
Wouter Lemcke

Reputation: 205

preg_match on hostname

I'm trying to make a regular expression that matches on hostname in the url. I want it to hit on de following hostnames: - www.site.nl - site.nl

I want it to return site.nl

I got this working with the following regex: (?(?<=www.))site.(?:nl|be|eu)

However when I use this regex on something.site.nl it also returns site.nl. This is not what I want. I only want a match when there is nothing before site.nl or when there's www. before it.

I've tried a lot of different expressions but can't figure out how I could do this.

I hope my question is clear and you guys can help me with this.

Upvotes: 2

Views: 1079

Answers (2)

Vipin Jain
Vipin Jain

Reputation: 1402

Please use this:

/^(www\.)?(site\.(nl|be|eu))$/

Upvotes: 0

PhiLho
PhiLho

Reputation: 41142

^(?:www\.)?(site\.(?:nl|be|eu))$

should work.

(Don't forget the special meaning of dot in REs...)

Upvotes: 2

Related Questions