Beispiel #1
0
        public static void Main()
        {
            // TryMatch() can get a single match
            if (IsoDate.TryMatch("2021-02-03T01:23:45.678Z", out var isoDate))
            {
                Console.WriteLine($"IsoDate: year={isoDate.Year} month={isoDate.Month} day={isoDate.Day}");
            }

            // IsMatch() allows simple validity checks
            var testUsernames = new[] { "foo-bar", "foobar", "3foobar", "foo-bar-", "-foo-bar", "foo--bar" };

            foreach (var username in testUsernames)
            {
                Console.WriteLine($"GithubUsername: {username}: {GithubUsername.IsMatch(username)}");
            }

            // Matches() allows iterating through several matches
            var sourceText =
                "This is some example text showing URL matching, for https://example.org. " +
                "It will match with IPs http://192.0.2.1 or ftp://some.site.example.org and" +
                "also long ones https://user:[email protected]:4433/some/path?query=abc&param=234.";

            foreach (var url in UrlRegex.Matches(sourceText))
            {
                Console.WriteLine($"Found URL, scheme = {url.Scheme}, fullhost = {url.Fullhost}");
                foreach (var path in url.Path)
                {
                    Console.WriteLine("  " + path);
                }
            }
        }
        public override List <ExtractResult> Extract(string text)
        {
            var ret        = base.Extract(text);
            var urlMatches = UrlRegex.Matches(text);

            foreach (Match urlMatch in urlMatches)
            {
                var tldString  = urlMatch.Groups["Tld"].Value;
                var tldMatches = TldMatcher.Find(tldString);

                if (tldMatches.Any(o => o.Start == 0 && o.End == tldString.Length))
                {
                    ret.Add(new ExtractResult
                    {
                        Start  = urlMatch.Index,
                        Length = urlMatch.Length,
                        Text   = urlMatch.Value,
                        Type   = ExtractType,
                        Data   = Constants.URL_REGEX
                    });
                }
            }

            return(ret);
        }
        public override IEnumerable <UTCourse> FetchItems()
        {
            List <UTCourse> results = new List <UTCourse>();

            if (this.Content == null)
            {
                return(results);
            }
            this.Content = this.Content.Replace("\n", String.Empty).Replace("\r", String.Empty);
            MatchCollection matches = CourseRegex.Matches(this.Content);

            foreach (Match match in matches)
            {
                UTCourse course = new UTCourse()
                {
                    Code           = match.Groups["code"].Value,
                    SemesterPrefix = match.Groups["prefix"].Value,
                    Name           = SpaceRegex.Replace(HttpUtility.HtmlDecode(match.Groups["name"].Value).Trim(' ').Trim(' '), " ")
                };
                string[] properties = AngleRegex.Replace(match.Groups["content"].Value.Replace("<br>", "|"), String.Empty).Split('|');

                string description      = String.Empty;
                bool   afterDescription = false;
                foreach (string property in properties)
                {
                    if (property.Trim(' ').StartsWith("Prerequisite: "))
                    {
                        course.Prerequisites = SpaceRegex.Replace(HttpUtility.HtmlDecode(property.Trim(' ').Substring("Prerequisite: ".Length)), " ");
                        afterDescription     = true;
                    }
                    else if (property.Trim(' ').StartsWith("Exclusion: "))
                    {
                        course.Exclusions = SpaceRegex.Replace(HttpUtility.HtmlDecode(property.Trim(' ').Substring("Exclusion: ".Length)), " ");
                        afterDescription  = true;
                    }
                    else if (property.Trim(' ').StartsWith("Breadth Requirement: "))
                    {
                        course.Exclusions = SpaceRegex.Replace(HttpUtility.HtmlDecode(property.Trim(' ').Substring("Breadth Requirement: ".Length)), " ");
                        afterDescription  = true;
                    }
                    else if (property.Trim(' ').StartsWith("Corequisite: "))
                    {
                        course.Corequisites = SpaceRegex.Replace(HttpUtility.HtmlDecode(property.Trim(' ').Substring("Corequisite: ".Length)), " ");
                        afterDescription    = true;
                    }
                    else if (!afterDescription)
                    {
                        description += property;
                    }
                }
                course.Description = description;
                course.Department  = SpaceRegex.Replace(HttpUtility.HtmlDecode(description), " ").Trim(' ');
                course.Department  = UrlRegex.Match(this.Url).Groups["name"].Value.Replace("_", " ");
                results.Add(course);
                //Console.WriteLine("UTSC Course: " + course.Abbr);
            }

            return(results);
        }
Beispiel #4
0
        public static void ParseUrls(MessageModel msg)
        {
            // clone MessageParts
            IList <MessagePartModel> parts = new List <MessagePartModel>(msg.MessageParts);

            foreach (MessagePartModel part in parts)
            {
                if (part is UrlMessagePartModel)
                {
                    // no need to reparse URL parts
                    continue;
                }
                if (!(part is TextMessagePartModel))
                {
                    continue;
                }

                TextMessagePartModel textPart = (TextMessagePartModel)part;
                Match urlMatch = UrlRegex.Match(textPart.Text);
                // OPT: fast regex scan
                if (!urlMatch.Success)
                {
                    // no URLs in this MessagePart, nothing to do
                    continue;
                }

                // found URL(s)
                // remove current MessagePartModel as we need to split it
                int idx = msg.MessageParts.IndexOf(part);
                msg.MessageParts.RemoveAt(idx);

                string[] textPartParts = textPart.Text.Split(new char[] { ' ' });
                for (int i = 0; i < textPartParts.Length; i++)
                {
                    string textPartPart = textPartParts[i];
                    urlMatch = UrlRegex.Match(textPartPart);
                    if (urlMatch.Success)
                    {
                        UrlMessagePartModel urlPart = new UrlMessagePartModel(textPartPart);
                        //urlPart.ForegroundColor = new TextColor();
                        msg.MessageParts.Insert(idx++, urlPart);
                        msg.MessageParts.Insert(idx++, new TextMessagePartModel(" "));
                    }
                    else
                    {
                        // FIXME: we put each text part into it's own object, instead of combining them (the smart way)
                        TextMessagePartModel notUrlPart = new TextMessagePartModel(textPartPart + " ");
                        // restore formatting / colors from the original text part
                        notUrlPart.IsHighlight     = textPart.IsHighlight;
                        notUrlPart.ForegroundColor = textPart.ForegroundColor;
                        notUrlPart.BackgroundColor = textPart.BackgroundColor;
                        notUrlPart.Bold            = textPart.Bold;
                        notUrlPart.Italic          = textPart.Italic;
                        notUrlPart.Underline       = textPart.Underline;
                        msg.MessageParts.Insert(idx++, notUrlPart);
                    }
                }
            }
        }
Beispiel #5
0
        private bool IsURLValid()
        {
            Match match = UrlRegex.Match(_url.Text);

            if (!match.Success)
            {
                return(false);
            }
            return(true);
        }
        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));
        }
Beispiel #7
0
            public Task CheckAsync(DiscordMessage message)
            {
                //Set some obvious
                MessageId = message.Id;

                //Detect URL
                var matches = UrlRegex.Match(message.Content);

                if (matches.Success)
                {
                    URL = matches.Value;
                }

                return(Task.CompletedTask);
            }
Beispiel #8
0
        public override RouteData GetRouteData(HttpContextBase httpContext)
        {
            var route = base.GetRouteData(httpContext);

            if (route != null)
            {
                httpContext.Response.Clear();
                httpContext.Response.StatusCode = _responseCode;

                var url     = GetRequestUrl(httpContext);
                var replace = UrlRegex.Replace(url, ReplacePattern);
                httpContext.Response.AppendHeader("Location", replace);
                httpContext.Response.End();
            }

            return(null);
        }
        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));
        }
        private static IEnumerable <string> GetUrlsInMessage(string messages)
        {
            var matches = UrlRegex.Matches(messages);

            return(from object match in matches select match.ToString());
        }
        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));
        }