-1

I want to validate a url that is with or without http. i tried ^http(s{0,1})://[a-zA-Z0-9_/\\-\\.]+\\.([A-Za-z/]{2,5})[a-zA-Z0-9_/\\&\\?\\=\\-\\.\\~\\%]*

But this is matches http://google.com but not www.google.com. i want a regex that matches www.google.com too.

Thanks

Romi
  • 4,833
  • 28
  • 81
  • 113
  • 1
    use a library if you can: http://commons.apache.org/validator/apidocs/org/apache/commons/validator/routines/UrlValidator.html – Amir T Dec 24 '12 at 04:45
  • If you research using regular expressions to match URLs you'll find that it is very difficult to create one that matches all cases. – Brian Roach Dec 24 '12 at 04:58

2 Answers2

3

Try starting your regex with ^(https?://)? instead of ^http(s{0,1})://

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
2

^(?:https?://)? ... the rest of your regex

  • ?: means do not capture group
  • ? quantifier minification (match if exists, if not - omit it)

P.S. I'm not sure if quantifier minification makes sense to english native speakers, this is a rough translation from russian :) Hopefully, if I'm mistaken, somebody understands what I meant and could fix me.

Nemoden
  • 8,816
  • 6
  • 41
  • 65