Regular Expression for URI

A while ago I had a task to fix regular expression for URI. What you will do first? Right, just open StackOverflow and find right solution. But I did not find. It is sadly, isn’t it?

Test strings

Good

Bad

  • test
  • 1234567www.facebook.com/first.second.14
  • .www.facebook.com/first.second.14
  • www.foufos
  • http://www.foufos
  • http://foufos
  • www.mp3#.com
  • www.foufos-.gr
  • www.-foufos.gr

Solution, that I purposed: ^(https?:\/\/)?(www.)?([a-z][a-z0-9]*\.)+[a-z]{2,5}(\/|(\/[a-zA-Z0-9#.&_\?=]+\/?))*$

On regex101 with test strings: URI regex

Maybe I forget something and make mistake? I will be glad to tips and corrections.

comments powered by Disqus