public void UrlRegex_DoNotMatchUrls([Values( "http://", "http://.", "http://..", "http://../", "http://?", "http://??", "http://??/", "http://#", "http://##", "http://##/", "http://foo.bar?q=Spaces should be encoded", "//", "//a", "///a", "///", "http:///a", "foo.com", "rdar://1234", "h://test", "http:// shouldfail.com", ":// should fail", "http://foo.bar/foo(bar)baz quux", "http://-error-.invalid/", "http://-a.b.co", "http://a.b-.co", "http://123.123.123", "http://3628126748", "http://.www.foo.bar/", "http://.www.foo.bar./", "http://go/ogle.com", "http://foo.bar/ /", "http://a.b_z.com", "http://ab_.z.com", "http://google\\.com", "http://www(google.com", "http://www.example.xn--overly-long-punycode-test-string-test-tests-123-test-test123/", "http://www=google.com", "https://www.g.com/error\n/bleh/bleh", "rdar://1234", "/foo.bar/", "///www.foo.bar./" )] [NotNull] string argument) { var regex = UrlRegex.Create(true); Assert.IsFalse(regex.IsMatch(argument)); }
public void UrlRegex_MatchUrlsInText([Values( @"Lorem ipsum //dolor.sit <a href=""http://example.com"">example.com</a> <a href=""http://example.com/with-path"">with path</a> [and another](https://another.example.com) and Foo //bar.net/?q=Query with spaces" )] [NotNull] string argument) { var regex = UrlRegex.Create(tlds: false, compiled: true); Assert.AreEqual(new[] { "//dolor.sit", "http://example.com", "http://example.com/with-path", "https://another.example.com", "//bar.net/?q=Query" }, argument.Matches(regex).Select(x => x.Value).ToArray()); Assert.IsTrue(regex.IsMatch(argument)); }
public void UrlRegex_MatchUsingListOfTlds([Values( "foo.com/blah_blah", "foo.com/blah_blah/", "foo.com/blah_blah_(wikipedia)", "foo.com/blah_blah_(wikipedia)_(again)", "www.example.com/wpstyle/?p=364", "www.example.com/foo/?bar=baz&inga=42&quux", "a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z.com", "mw1.google.com/mw-earth-vectordb/kml-samples/gp/seattle/gigapxl/$[level]/r$[y]_c$[x].jpg", "user:[email protected]:123/one/two.three?q1=a1&q2=a2#body", "www.microsoft.xn--comindex-g03d.html.irongeek.com", "✪df.ws/123", "localhost/", "userid:[email protected]:8080", "userid:[email protected]:8080/", "*****@*****.**", "[email protected]/", "[email protected]:8080", "[email protected]:8080/", "userid:[email protected]", "userid:[email protected]/", "142.42.1.1/", "142.42.1.1:8080/", "➡.ws/䨹", "⌘.ws", "⌘.ws/", "foo.com/blah_(wikipedia)#cite-1", "foo.com/blah_(wikipedia)_blah#cite-1", "foo.com/unicode_(✪)_in_parens", "foo.com/(something)?after=parens", "☺.damowmow.com/", "code.google.com/events/#&product=browser", "j.mp", "foo.bar/baz", "foo.bar/?q=Test%20URL-encoded%20stuff", "-.~_!$&\"()*+\";=:%40:80%2f::::::@example.com", "1337.net", "a.b-c.de", "223.255.255.254", "example.com?foo=bar", "example.com#foo", "localhost:8080", "foo.ws", "a.b-c.de", "223.255.255.254", "userid:[email protected]", "➡.ws/䨹", "//localhost:8080", "//foo.ws", "//a.b-c.de", "//223.255.255.254", "//userid:[email protected]", "//➡.ws/䨹", "www.google.com/unicorn", "example.com." )] [NotNull] string argument) { var regex = UrlRegex.Create(true, false, false); Assert.IsTrue(regex.IsMatch(argument)); }