コード例 #1
0
        public void TestAlternationNodeRendering()
        {
            RegexNodeLiteral literal1 = new RegexNodeLiteral(@"\w*");
            RegexNodeLiteral literal2 = new RegexNodeLiteral(@"\d+");
            RegexNodeLiteral literal3 = new RegexNodeLiteral(@"\s?");

            RegexNodeAlternation alternation1 = new RegexNodeAlternation(literal1, literal2);

            Assert.AreEqual(@"(?:\w*|\d+)", alternation1.ToRegexPattern());

            RegexNodeAlternation alternation2 = new RegexNodeAlternation(literal1, literal2, literal3);

            Assert.AreEqual(@"(?:\w*|\d+|\s?)", alternation2.ToRegexPattern());

            RegexNodeAlternation alternation3 = RegexBuilder.Alternate(literal1, literal2);

            Assert.AreEqual(@"(?:\w*|\d+)", alternation3.ToRegexPattern());

            RegexNodeAlternation alternation4 = RegexBuilder.Alternate(new RegexNode[] { literal1, literal2, literal3 });

            Assert.AreEqual(@"(?:\w*|\d+|\s?)", alternation4.ToRegexPattern());

            RegexNodeAlternation alternation5 = new RegexNodeAlternation(literal1, literal2);

            alternation5.Quantifier = RegexQuantifier.ZeroOrMore;
            Assert.AreEqual(@"(?:\w*|\d+)*", alternation5.ToRegexPattern());

            RegexNodeAlternation alternation6 = new RegexNodeAlternation(literal1, literal2, literal3);

            alternation6.Quantifier = RegexQuantifier.OneOrMore;
            Assert.AreEqual(@"(?:\w*|\d+|\s?)+", alternation6.ToRegexPattern());

            RegexNodeAlternation alternation7 = RegexBuilder.Alternate(literal1, literal2, RegexQuantifier.ZeroOrOne);

            Assert.AreEqual(@"(?:\w*|\d+)?", alternation7.ToRegexPattern());

            RegexNodeAlternation alternation8 = RegexBuilder.Alternate(new RegexNode[] { literal1, literal2, literal3 }, RegexQuantifier.AtLeast(5));

            Assert.AreEqual(@"(?:\w*|\d+|\s?){5,}", alternation8.ToRegexPattern());
        }
コード例 #2
0
        public void TestGroupReferenceNodeRendering()
        {
            RegexNodeGroupReference reference1 = new RegexNodeGroupReference(1);

            Assert.AreEqual(@"\1", reference1.ToRegexPattern());

            RegexNodeGroupReference reference2 = new RegexNodeGroupReference("SomeGroup");

            Assert.AreEqual(@"\k<SomeGroup>", reference2.ToRegexPattern());

            RegexNodeGroupReference reference3 = RegexBuilder.GroupBackReference("SomeGroup");

            Assert.AreEqual(@"\k<SomeGroup>", reference3.ToRegexPattern());

            RegexNodeGroupReference reference4 = RegexBuilder.GroupBackReference("SomeGroup", RegexQuantifier.Exactly(2));

            Assert.AreEqual(@"\k<SomeGroup>{2}", reference4.ToRegexPattern());

            RegexNodeGroupReference reference5 = RegexBuilder.GroupBackReference(1, RegexQuantifier.ZeroOrMore);

            Assert.AreEqual(@"\1*", reference5.ToRegexPattern());
        }
コード例 #3
0
        public void TestEmailValidator()
        {
            const string quotationMark          = "\"";
            const string alphaCharacters        = "a-zA-Z";
            const string alphaNumericCharacters = "0-9a-zA-Z";
            const string allowedLoginSymbols    = "-!#$%&'*+=?^`{}|~";

            // Regex value taken from MSDN: http://msdn.microsoft.com/en-us/library/01escwtf.aspx
            Regex emailRegex = RegexBuilder.Build
                               (
                RegexBuilder.MetaCharacter(RegexMetaChars.LineStart),
                // Match everything before @
                RegexBuilder.ConditionalMatch
                (
                    // If the string starts with a quotation mark...
                    RegexBuilder.Literal(quotationMark),
                    // ...then match the quoted text and the @ character...
                    RegexBuilder.Concatenate
                    (
                        RegexBuilder.Literal(quotationMark),
                        RegexBuilder.MetaCharacter(RegexMetaChars.AnyCharacter, RegexQuantifier.OneOrMoreLazy),
                        RegexBuilder.Literal(quotationMark),
                        RegexBuilder.Literal("@")
                    ),
                    // ...otherwise, match a sequence of alphanumeric characters and symbols, followed by @
                    RegexBuilder.Concatenate
                    (
                        RegexBuilder.CharacterSet(alphaNumericCharacters, RegexQuantifier.None),
                        RegexBuilder.Alternate
                        (
                            // The sequence can either contain only one dot...
                            RegexBuilder.NegativeLookAhead
                            (
                                RegexBuilder.Literal("."),
                                RegexBuilder.Literal(".")
                            ),
                            // ...or contain some special symbols (-!#$%&'*+=?^`{}|~)
                            RegexBuilder.CharacterSet(allowedLoginSymbols + RegexMetaChars.WordCharacter, RegexQuantifier.ZeroOrMore),

                            RegexQuantifier.ZeroOrMore
                        ),
                        // Before matching the @ character, make sure that it is preceded by alphanumeric characters.
                        RegexBuilder.PositiveLookBehind
                        (
                            RegexBuilder.CharacterSet(alphaNumericCharacters, RegexQuantifier.None),
                            RegexBuilder.Literal("@")
                        )
                    )
                ),
                // Match everything after @
                RegexBuilder.ConditionalMatch
                (
                    // Domain can be either an IP address enclosed in square brackets...
                    RegexBuilder.Literal("["),
                    // ...(IP address should look like [aaa.bbb.ccc.ddd])...
                    RegexBuilder.Concatenate
                    (
                        RegexBuilder.Literal("["),
                        RegexBuilder.Concatenate
                        (
                            RegexBuilder.MetaCharacter(RegexMetaChars.Digit, RegexQuantifier.Custom(1, 3, false)),
                            RegexBuilder.Literal("."),
                            RegexQuantifier.Exactly(3)
                        ),
                        RegexBuilder.MetaCharacter(RegexMetaChars.Digit, RegexQuantifier.Custom(1, 3, false)),
                        RegexBuilder.Literal("]")
                    ),
                    // ...or it can be a hostname.
                    RegexBuilder.Concatenate
                    (
                        RegexBuilder.Concatenate
                        (
                            RegexBuilder.CharacterSet(alphaNumericCharacters, RegexQuantifier.None),
                            RegexBuilder.CharacterSet("-" + RegexMetaChars.WordCharacter, RegexQuantifier.ZeroOrMore),
                            RegexBuilder.CharacterSet(alphaNumericCharacters, RegexQuantifier.None),
                            RegexBuilder.Literal("."),
                            RegexQuantifier.OneOrMore
                        ),
                        RegexBuilder.CharacterSet(alphaCharacters, RegexQuantifier.Custom(2, 6, false))
                    )
                ),
                RegexBuilder.MetaCharacter(RegexMetaChars.LineEnd)
                               );

            Assert.IsTrue(emailRegex.IsMatch("*****@*****.**"));
            Assert.IsTrue(emailRegex.IsMatch("*****@*****.**"));
            Assert.IsTrue(emailRegex.IsMatch("*****@*****.**"));
            Assert.IsFalse(emailRegex.IsMatch("*****@*****.**"));
            Assert.IsFalse(emailRegex.IsMatch("[email protected]"));
            Assert.IsTrue(emailRegex.IsMatch("js#[email protected]"));
            Assert.IsTrue(emailRegex.IsMatch("j_9@[129.126.118.1]"));
            Assert.IsFalse(emailRegex.IsMatch("*****@*****.**"));
            Assert.IsFalse(emailRegex.IsMatch("js*@proseware.com"));
            Assert.IsFalse(emailRegex.IsMatch("*****@*****.**"));
            Assert.IsFalse(emailRegex.IsMatch("[email protected]"));
            Assert.IsTrue(emailRegex.IsMatch("*****@*****.**"));
        }