3

I have such regex:

/^(?!((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?))/

When I type: www.test.com - it doesn't find anything (as should be)

When I type: aaaaa - it's valid (it's true)

But why when I type:

wwwww - I get invalid regex? (I mean it doesn't match regex)

What am I doing wrong in my regex?

チーズパン
  • 2,752
  • 8
  • 42
  • 63
j.doe
  • 41
  • 2
  • 2
    Paste it into regex101.com and debug. – Wiktor Stribiżew Jun 08 '16 at 09:16
  • It's a negative lookahead `(?!` that among other stuff doesn't want to find something that starts with www. Indeed, debug and perhaps try to simplify that long regex. The regexr.com site has a nice reference. – LukStorms Jun 08 '16 at 09:35
  • @WiktorStribiżew how can i debug it? what do you mean? – j.doe Jun 08 '16 at 09:55
  • 1
    This question is of the type What does this regex mean. The answer is the same: paste the test string to the input field and regex to the regex field at regex101.com (or use Expresso for .NET regex testing) and read the description. Then delete all but one subpatterns to see how the regex behaves. Add the x modifier to insert newlines into the pattern and indent it to better see what groups you have. Test, test, test. – Wiktor Stribiżew Jun 08 '16 at 10:01
  • regex101.com -- awesome – Nicholas Hamilton Jun 08 '16 at 10:33

2 Answers2

0

It looks like you are trying to build a URI regex. I can link a good-enough version here (I don't use this one but it might work for you):

/^(?:(\w[\w\d+.-]+):\/\/)?(?:(.+?):(.+)@)?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|[^#:\/\?\s]+)(?::(\d+))?\/?(\/(?:[^#\/\?\s]+\/)*[^#\/\?\s]+\/?)?(?:\?((?:[^#=\/\s]+=[^&;#\/\s]+[&;]?)+))?(?:#(.+))?$/g

http://regexr.com/3cmin

But I suggest you have a look at these https://mathiasbynens.be/demo/url-regex

http://jmrware.com/articles/2009/uri_regexp/URI_regex.html

Regular expression to match generic URL

The official standard for URIs is RFC-3986

Otherwise just use regexr.com to debug your regex and regex101.com to explore it.

As was mentioned above you did not escape the . after www. Also your regex is infinite because it can match zero characters. You need to put a $ at the end at least to indicate end of match. And you are turning your regex into a lookahead right off the bat.

Community
  • 1
  • 1
yosefrow
  • 2,128
  • 20
  • 29
0

I think you forgot to escape the period after www. Try replacing www. with www\..

naktinis
  • 3,957
  • 3
  • 36
  • 52