-1

I need to check URL. This examples must be true:

http://www.stackoverflow.com
http://stackoverflow.com

But this must not:

http:/www.stackoverflow

I tried to achieve it with RegEx:

http://(www\.)?.+\..+

But it is not working. All examples are true. This regex don't pay attention on (www\.) part. How can I solve this problem?

  • You're forgetting about `https:`. Also, `http://www.com` is a valid URL. – SLaks Feb 27 '17 at 23:05
  • 4
    Possible duplicate of [What is the best regular expression to check if a string is a valid URL?](http://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url) – Nullman Feb 27 '17 at 23:06
  • this regex works fine for the given examples, BUT you should escape the forward slashes – Nullman Feb 27 '17 at 23:07
  • 1
    Please search thoroughly before asking questions. – behkod Feb 27 '17 at 23:25
  • Just to note, sites like http://www.gl and http://www.com DO exist.. and since TLDs can be up to 63 characters (24 is the longest I've heard of), URL matching has gotten a lot harder. Or you can take the legitimate path of ignoring the few scenarios of ignoring `www` domains. – Regular Jo Feb 28 '17 at 02:29

3 Answers3

0

You have to escape the forward slashes:

http:\/\/(www\.)?.+\..+
Eddie Martinez
  • 13,582
  • 13
  • 81
  • 106
0

(https?:\/\/)?(www\.)?\S+\.\S+

Tim.Tang
  • 3,158
  • 1
  • 15
  • 18
0

Try this: http:\/\/(www.)?.+\..+[a-z]

Or if you also want to allow https: http(s)?:\/\/(www.)?.+\..+[a-z]

Reza Saadati
  • 1,224
  • 13
  • 27