0

Here is my regex that tries to match a valid URL:

^(https?:\/\/((\b\w[^-][a-zA-Z0-9-]{1,33})\.){1,34}([[a-zA-Z0-9]{1,6})\/?)$

and I've tried to find way to make a simple solution to forbid the use of a hyphen '-' in the beginning and end of a group of letters and numbers. I'd tried to use \b\w[^-]. But it hasn't helped. For example, my regex matches this string, but it shouldn't http://example-.com

ajc2000
  • 651
  • 5
  • 20
  • Try `(?i)^(?!mailto:)(?:(?:https?|ftp):\/\/)?(?:\S+(?::\S*)?@)?(?:(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))|localhost)(?::\d{2,5})?(?:\/[^\s]*)?$` –  Jul 26 '17 at 19:00

1 Answers1

0

I found an answer by myself

^(https?:\/\/([WWW\.]|[www\.])?((([a-zA-Z0-9]|[a-zA-Z0-9][-][a-zA-Z0-9]){1,10})\.){1,33}([[a-zA-Z0-9]{1,6})\/?)$

its just a simple OR [a-zA-Z0-9]|[a-zA-Z0-9][-][a-zA-Z0-9]

If you'll find better solution pls send it :)

Toto
  • 89,455
  • 62
  • 89
  • 125
  • 1
    FYI: `[WWW\.]` matches one uppercase `W` or one dot. I'm pretty sure that is not what you want. – Toto Jul 26 '17 at 17:13