shabby
shabby

Reputation: 3222

Matching a web address through regex

I want to match a web address through regex which should capture http://www.google.com as well as www.google.com i.e. with and without protocol.

Upvotes: 0

Views: 9959

Answers (4)

Igor
Igor

Reputation: 27258

Why not

/google\.com/

?

It catches http://www.google.com , www.google.com , and even google.com for free! :-)

Upvotes: 0

annakata
annakata

Reputation: 75862

Well it's going to depend on exactly what you want to capture ("FTP"? "/index.htm"?) because a general URI capture based on the RFC standard is very hard, but you could start with:

/^((https?\:\/\/)?([\w\d\-]+\.){2,}([\w\d]{2,})((\/[\w\d\-\.]+)*(\/[\w\d\-]+\.[\w\d]{3,4}(\?.*)?)?)?)$/

Complicated see?

Upvotes: 3

dirkgently
dirkgently

Reputation: 111200

Read RFC 3986. It is not just as easy as you might think it is. The job is easier if you only have a small set of URLs to parse.

Upvotes: 1

Mitch Wheat
Mitch Wheat

Reputation: 300719

Try RegexLib.

Upvotes: 2

Related Questions