Пример #1
0
        public void TestOptionGroup()
        {
            Assert.AreEqual(new OptionsGroupPattern().Expression, "");
            var op = new OptionsGroupPattern().IgnoreCase().Multiline(false);

            Assert.AreEqual(op.Expression, "(?i-m)");

            op = new OptionsGroupPattern().SetAll(true);
            Assert.AreEqual(op.Expression, "(?imnsx)");

            op = new OptionsGroupPattern("").SetAll(false);
            Assert.AreEqual(op.Expression, "(?-imnsx)");

            op = new OptionsGroupPattern("abc");
            Assert.AreEqual(op.Expression, "(abc)");

            op = new OptionsGroupPattern("abc").ExplicitCapture(false).SingleLine();
            Assert.AreEqual(op.Expression, "(?s-n:abc)");

            op = new OptionsGroupPattern("abc").SetAll(true);
            Assert.AreEqual(op.Expression, "(?imnsx:abc)");

            op = new OptionsGroupPattern("abc").SetAll(false);
            Assert.AreEqual(op.Expression, "(?-imnsx:abc)");
        }
Пример #2
0
        public void TestStatics()
        {
            ut.Assert.AreEqual(Text("abc", ".txt").Expression, @"abc\.txt");
            ut.Assert.AreEqual(Text("abc", ".txt")[1].Expression, @"(?:abc\.txt){1}");
            ut.Assert.AreEqual(Group("abc", ".txt").Expression, @"(abc\.txt)");
            ut.Assert.AreEqual(NamedGroup("group1", "abc", ".txt").AtLeast(3).Expression, @"(?'group1'abc\.txt){3,}");
            ut.Assert.AreEqual(CaptureGroup("group1", "abc", ".txt").NoneOrOnce(RepeatMode.Lazy).Expression, @"(?'group1'abc\.txt)??");
            var g = NamedGroup("group1", NewLine.OnceOrMore());

            ut.Assert.AreEqual((g + BackRef("group1")).Expression, @"(?'group1'(?:\n|\r\n)+)\k'group1'");
            ut.Assert.AreEqual((g + BackRef(1)).Expression, @"(?'group1'(?:\n|\r\n)+)\1");

            ut.Assert.AreEqual(AtomicGroup("abc", ".txt").NoneOrMany().Expression, @"(?>abc\.txt)*");
            ut.Assert.AreEqual(Greedy("abc", ".txt").Maybe().Expression, @"(?>abc\.txt)?");
            ut.Assert.AreEqual(NonBacktracking("abc", ".txt").OnceOrMore().Expression, @"(?>abc\.txt)+");
            ut.Assert.AreEqual(NoBacktrack("abc", ".txt").Expression, @"(?>abc\.txt)");

            ut.Assert.AreEqual(NonCapturingGroup("abc", ".txt").Expression, @"(?:abc\.txt)");
            ut.Assert.AreEqual(Enclose("abc", ".txt")[1].Expression, @"(?:abc\.txt){1}");

            ut.Assert.AreEqual(BalancingGroup("group1", "abc.txt").Expression, @"(?'-group1'abc\.txt)");
            ut.Assert.AreEqual(BalancingGroup("group1", "group2", "abc.txt").Expression, @"(?'group2-group1'abc\.txt)");


            ut.Assert.AreEqual(LookAhead("abc")[2, 7].Expression, "(?=abc)");
            ut.Assert.AreEqual(NegativeLookAhead("abc", "efg").Expression, "(?!abcefg)");
            ut.Assert.AreEqual(LookBehind("abc".Repeat(5)).Expression, "(?<=(?:abc){5})");
            ut.Assert.AreEqual(NegativeLookBehind("abc".AsItIs() + "d" | "f").Expression, "(?<!abcd|f)");

            ut.Assert.AreEqual(AssertNextIs("abc").Expression, "(?=abc)");
            ut.Assert.AreEqual(AssertNextIsNot("abc", "efg")[2, 7].Expression, "(?!abcefg)");
            ut.Assert.AreEqual(AssertPreviousIs("abc".Repeat(5)).Expression, "(?<=(?:abc){5})");
            ut.Assert.AreEqual(AssertPreviousIsNot("abc".AsItIs() + "d" | "f").Expression, "(?<!abcd|f)");

            var p = "q" + AssertNextIs("u" + Group(AnyChar)) +
                    AnyChar.Repeat(3) + BackRef(1);

            ut.Assert.AreEqual(p.Expression, @"q(?=u(.)).{3}\1");

            p = "qu" + Group(AnyChar) + AnyChar + BackRef(1);
            ut.Assert.AreEqual(p.Expression, @"qu(.).\1");

            p = "qu" + Group(AnyChar) + AnyChar + AssertNextIs(BackRef(1));
            ut.Assert.AreEqual(p.Expression, @"qu(.).(?=\1)");

            ut.Assert.AreEqual(Intersection(Unicode.Arabic, Unicode.Letter).Expression, @"(?=\p{IsArabic})\p{L}");
            ut.Assert.AreEqual(Difference(Unicode.Letter, Unicode.Arabic).Expression, @"(?!\p{IsArabic})\p{L}");

            var ag = IfExists("group1").ThenMatch("abc");

            ut.Assert.AreEqual(ag.Expression, "(?(group1)abc)");
            ag = ag.ElseMatch("x");
            ut.Assert.AreEqual(ag.Expression, "(?(group1)abc|x)");

            ag = Check(1).YesPattern("abc");
            ut.Assert.AreEqual(ag.Expression, "(?(1)abc)");
            ag = ag.NoPattern("x");
            ut.Assert.AreEqual(ag.Expression, "(?(1)abc|x)");

            ag = Assert(StartOfText).YesPattern("abc");
            ut.Assert.AreEqual(ag.Expression, @"(?(\A)abc)");
            ag = ag.NoPattern("x");
            ut.Assert.AreEqual(ag[2, 3].Expression, @"(?(\A)abc|x){2,3}");

            ut.Assert.AreEqual(OptionsGroup()[1].Expression, "");
            var op = new OptionsGroupPattern().IgnoreCase().Multiline(false);

            ut.Assert.AreEqual(op.Expression, "(?i-m)");

            p = OptionsGroup().SetAll(true).OnceOrMore();
            ut.Assert.AreEqual(p.Expression, "(?imnsx)");

            op = Options.SetAll(false);
            ut.Assert.AreEqual(op.Expression, "(?-imnsx)");

            op = Options.SetAll(null);
            ut.Assert.AreEqual(op.Expression, "");

            p = OptionsGroup("abc")[2, 3];
            ut.Assert.AreEqual(p.Expression, "(abc)");


            ut.Assert.AreEqual(Comment("Test").Expression, @"(?#Test)");

            var LineComments = WithLineComments(
                ("abc", "string"),
                (Unicode.Arabic, "Arabic letter"))[2];