godzilla
godzilla

Reputation: 3125

regular expression match domain

I need a regular expression to match the following domains as follows:

http://www.cnn.com/fred      = www.cnn.com
cnn.com                      = cnn.com
www.cnn.com:8080             = www.cnn.com

I have the following regular expression (using pcre):

([^/]+://)?([^:/]+)

The above works fine in case 2 and 3 however with 1 i still have the http:// appended to the matching string, is there a regular expression option which i can use to skip the http part?

many thanks in advance

Upvotes: 2

Views: 830

Answers (2)

godzilla
godzilla

Reputation: 3125

this looks like the closest i could get to what i want not perfect but seems to gets the job done

www?([^/:]+)

Upvotes: 0

sp00m
sp00m

Reputation: 48837

This one should suit your needs:

^(?:(?:f|ht)tps?://)?([^/:]+)

The first group will contain what you're looking for.

Upvotes: 1

Related Questions