Example #1
0
        public void ZeroWidthNegativeLookbehindAssertion()
        {
            var regex = RegularExpression.Of(
                NegativeLookbehindAssertion.Of(Literal.For("A"))
                ).ToRegex();

            Assert.AreEqual("(?<!A)", regex.ToString());
        }
Example #2
0
        public void NonbacktrackingExpression()
        {
            var regex = RegularExpression.Of(
                NonbacktrackingAssertion.Of(Literal.For("A"))
                ).ToRegex();

            Assert.AreEqual("(?>A)", regex.ToString());
        }
Example #3
0
        public void ZeroWidthPositiveLookaheadAssertion()
        {
            var regex = RegularExpression.Of(
                PositiveLookaheadAssertion.Of(Literal.For("A"))
                ).ToRegex();

            Assert.AreEqual("(?=A)", regex.ToString());
        }
Example #4
0
        public void OptionsGroup_NoOptions()
        {
            var regex = RegularExpression.Of(
                OptionsGroup.Of(GroupRegexOptions.None, GroupRegexOptions.None, Literal.For("A"))
                ).ToRegex();

            Assert.AreEqual("(?:A)", regex.ToString());
        }
Example #5
0
        public void NonCaptureGroup_Literal()
        {
            var regex = RegularExpression.Of(
                NonCaptureGroup.Of(Literal.For("A"))
                ).ToRegex();

            Assert.AreEqual("(?:A)", regex.ToString());
        }
        public void Alternation_ByExpression()
        {
            var regex = RegularExpression.Of(
                ConditionalAlternation.For(Literal.For("A"), Literal.For("Abc"), Literal.For("Xyz"))
                ).ToRegex();

            Assert.AreEqual("(?(A)Abc|Xyz)", regex.ToString());
        }
Example #7
0
        public void OptionsGroup_EnableOnly()
        {
            var enabled = GroupRegexOptions.IgnoreCase | GroupRegexOptions.Multiline | GroupRegexOptions.ExplicitCapture | GroupRegexOptions.IgnorePatternWhitespace;
            var regex   = RegularExpression.Of(
                OptionsGroup.Of(enabled, GroupRegexOptions.None, Literal.For("A"))
                ).ToRegex();

            Assert.AreEqual("(?imnx:A)", regex.ToString());
        }
        public void Alternation_Multiple()
        {
            var regex = RegularExpression.Of(
                Alternation.Of(
                    Literal.For("a"), Literal.For("b"), Literal.For("c")
                    )
                ).ToRegex();

            Assert.AreEqual("a|b|c", regex.ToString());
        }
Example #9
0
        public void Backreference_Number()
        {
            var regex = RegularExpression.Of(
                CaptureGroup.Of(Quantifiers.OneOrMore(Literal.For("a"))),
                Quantifiers.ZeroOrMore(Literal.For("b")),
                Backreference.For(1)
                ).ToRegex();

            Assert.AreEqual(@"(a+)b*\1", regex.ToString());
        }
        public void LeadingCarot_Negated_Matches()
        {
            var regex = RegularExpression.Of(
                CharacterGroup.Of(new CharacterGroupOptions()
            {
                IsNegated = true
            }, Literal.For("^"))
                ).ToRegex();

            Assert.IsFalse(regex.IsMatch("^"));
        }
Example #11
0
        public void Alternation_ByCapture()
        {
            var regex = RegularExpression.Of(
                CaptureGroup.Of(new CaptureGroupOptions()
            {
                Name = "A"
            }, Quantifiers.OneOrMore(Literal.For("A"))),
                ConditionalAlternation.For(Literal.For("A"), Literal.For("Abc"), Literal.For("Xyz"))
                ).ToRegex();

            Assert.AreEqual("(?<A>A+)(?(A)Abc|Xyz)", regex.ToString());
        }
Example #12
0
        public void BalancedGroup_AngleBrackets()
        {
            var regex = RegularExpression.Of(
                CaptureGroup.Of(new CaptureGroupOptions()
            {
                Name = "Open"
            }, Literal.For("<")),
                BalancedGroup.Of("Close", "Open", Literal.For(">"))
                ).ToRegex();

            Assert.AreEqual("(?<Open><)(?<Close-Open>>)", regex.ToString());
        }
Example #13
0
        public void Backreference_Name_WithQuotes()
        {
            var regex = RegularExpression.Of(
                CaptureGroup.Of(new CaptureGroupOptions()
            {
                Name = "a", UseQuotes = true
            }, Quantifiers.OneOrMore(Literal.For("a"))),
                Quantifiers.ZeroOrMore(Literal.For("b")),
                Backreference.For("a", true)
                ).ToRegex();

            Assert.AreEqual(@"(?'a'a+)b*\k'a'", regex.ToString());
        }
Example #14
0
        public void Backreference_Name()
        {
            var regex = RegularExpression.Of(
                CaptureGroup.Of(new CaptureGroupOptions()
            {
                Name = "a"
            }, Quantifiers.OneOrMore(Literal.For("a"))),
                Quantifiers.ZeroOrMore(Literal.For("b")),
                Backreference.For("a")
                ).ToRegex();

            Assert.AreEqual(@"(?<a>a+)b*\k<a>", regex.ToString());
        }
Example #15
0
        public void CaptureGroup_Name()
        {
            var regex = RegularExpression.Of(
                CaptureGroup.Of(
                    new CaptureGroupOptions()
            {
                Name = "A"
            },
                    Literal.For("A")
                    )
                ).ToRegex();

            Assert.AreEqual("(?<A>A)", regex.ToString());
        }
Example #16
0
        public void CaptureGroup_Name_WithQuotes()
        {
            var regex = RegularExpression.Of(
                CaptureGroup.Of(
                    new CaptureGroupOptions()
            {
                Name = "A", UseQuotes = true
            },
                    Literal.For("A")
                    )
                ).ToRegex();

            Assert.AreEqual("(?'A'A)", regex.ToString());
        }
Example #17
0
        public void BalancedGroup_WithQuotes()
        {
            var regex = RegularExpression.Of(
                CaptureGroup.Of(new CaptureGroupOptions()
            {
                Name = "Open", UseQuotes = true
            }, Literal.For("<")),
                BalancedGroup.Of("Close", "Open", new BalanceGroupOptions()
            {
                UseQuotes = true
            }, Literal.For(">"))
                ).ToRegex();

            Assert.AreEqual("(?'Open'<)(?'Close-Open'>)", regex.ToString());
        }
Example #18
0
        public void UrlScheme()
        {
            var expression = RegularExpression.Of(
                Anchors.Carot,

                // scheme - (?:([A-Za-z]+):)?
                Quantifiers.ZeroOrOne(
                    NonCaptureGroup.Of(
                        CaptureGroup.Of(
                            Quantifiers.OneOrMore(
                                CharacterGroup.Of(
                                    Range.For(Literal.For('A'), Literal.For('Z')),
                                    Range.For(Literal.For('a'), Literal.For('z'))
                                    )
                                )
                            ),
                        Literal.For(":")
                        )
                    ),

                // slash - (/{0,3})
                CaptureGroup.Of(
                    Quantifiers.Between(Literal.For("/"), 0, 3)
                    ),

                // host - ([0-9.\-A-Za-z]+)
                CaptureGroup.Of(
                    Quantifiers.OneOrMore(
                        CharacterGroup.Of(
                            Range.For(Literal.For('0'), Literal.For('9')),
                            Literal.For("."),
                            Literal.For("-"),
                            Range.For(Literal.For('A'), Literal.For('Z')),
                            Range.For(Literal.For('a'), Literal.For('z'))
                            )
                        )
                    ),

                // port - (?::(\d+))?
                Quantifiers.ZeroOrOne(
                    NonCaptureGroup.Of(
                        Literal.For(":"),
                        CaptureGroup.Of(
                            Quantifiers.OneOrMore(CharacterClasses.Digit)
                            )
                        )
                    ),

                // path - (/[^?#]*)?
                Quantifiers.ZeroOrOne(
                    CaptureGroup.Of(
                        Literal.For("/"),
                        Quantifiers.ZeroOrMore(
                            CharacterGroup.Of(
                                new CharacterGroupOptions()
            {
                IsNegated = true
            },
                                Literal.For("?"),
                                Literal.For("#")
                                )
                            )
                        )
                    ),

                // query - (?:\?([^#]*))?
                Quantifiers.ZeroOrOne(
                    NonCaptureGroup.Of(
                        Literal.For("?"),
                        CaptureGroup.Of(
                            Quantifiers.ZeroOrMore(
                                CharacterGroup.Of(new CharacterGroupOptions()
            {
                IsNegated = true
            }, Literal.For("#"))
                                )
                            )
                        )
                    ),

                // hash - (?:#(.*))?
                Quantifiers.ZeroOrOne(
                    NonCaptureGroup.Of(
                        Literal.For("#"),
                        CaptureGroup.Of(
                            Quantifiers.ZeroOrMore(CharacterClasses.Wildcard)
                            )
                        )
                    ),

                Anchors.Dollar
                );

            var regex  = expression.ToRegex();
            var source = regex.ToString();

            const string urlRegex = @"^(?:([A-Za-z]+):)?(/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(/[^?#]*)?(?:\?([^#]*))?(?:\#(.*))?$";

            Assert.AreEqual(urlRegex, source);
        }
        public void LeadingCarot_Matches()
        {
            var regex = RegularExpression.Of(CharacterGroup.Of(Literal.For("^"))).ToRegex();

            Assert.IsTrue(regex.IsMatch("^"));
        }
        public void EmbeddedDash_Escapes_Matches()
        {
            var regex = RegularExpression.Of(CharacterGroup.Of(Literal.For("a-z"))).ToRegex();

            Assert.IsTrue(regex.IsMatch("-"));
        }
        public void TrailingDash_Matches()
        {
            var regex = RegularExpression.Of(CharacterGroup.Of(Literal.For("abc-"))).ToRegex();

            Assert.IsTrue(regex.IsMatch("-"));
        }