Example #1
0
        public void should_detect_invalid_offset_limit_usage()
        {
            var re = new PcreRegex(@"bar");

            Assert.Throws <PcreMatchException>(() => re.Match("foobar", new PcreMatchSettings
            {
                OffsetLimit = 3
            }));
        }
Example #2
0
        public void should_allow_duplicate_names()
        {
            var re    = new PcreRegex(@"(?<g>a)?(?<g>b)?(?<g>c)?", PcreOptions.DupNames);
            var match = re.Match("b");

            Assert.That(match, Is.Not.Null);
            Assert.That(match.Success, Is.True);
            Assert.That(match["g"].Value, Is.EqualTo("b"));

            Assert.That(match.GetDuplicateNamedGroups("g").Select(g => g.Success), Is.EqualTo(new[] { false, true, false }));

            match = re.Match("bc");
            Assert.That(match, Is.Not.Null);
            Assert.That(match.Success, Is.True);
            Assert.That(match["g"].Value, Is.EqualTo("b"));

            Assert.That(match.GetDuplicateNamedGroups("g").Select(g => g.Success), Is.EqualTo(new[] { false, true, true }));
        }
Example #3
0
        public void should_detect_duplicate_names_ref()
        {
            var re = new PcreRegex(@"(?J)(?<g>a)?(?<g>b)?(?<g>c)?");

            var match = re.Match("bc".AsSpan());

            Assert.That(match.Success, Is.True);
            Assert.That(match["g"].Value.ToString(), Is.EqualTo("b"));

            Assert.That(GetDuplicateNamedGroupsSuccesses(match, "g"), Is.EqualTo(new[] { false, true, true }));
        }
Example #4
0
        public void should_handle_offset_limit()
        {
            var re = new PcreRegex(@"bar", PcreOptions.UseOffsetLimit);

            var match = re.Match("foobar");

            Assert.That(match.Success, Is.True);

            match = re.Match("foobar", new PcreMatchSettings
            {
                OffsetLimit = 3
            });
            Assert.That(match.Success, Is.True);

            match = re.Match("foobar", new PcreMatchSettings
            {
                OffsetLimit = 2
            });
            Assert.That(match.Success, Is.False);
        }
Example #5
0
        public void should_handle_end_before_start_ref()
        {
            var re = new PcreRegex(@"(?=a+\K)");

            var match = re.Match("aaa".AsSpan());

            Assert.That(match.Success, Is.True);
            Assert.That(match.Index, Is.EqualTo(3));
            Assert.That(match.EndIndex, Is.EqualTo(0));
            Assert.That(match.Length, Is.EqualTo(0));
            Assert.That(match.Value.ToString(), Is.EqualTo(string.Empty));
        }
Example #6
0
        public void should_execute_aborting_callout_ref()
        {
            var re = new PcreRegex(@".(?C42)");

            var match = re.Match("ab".AsSpan(), data =>
            {
                Assert.That(data.Number, Is.EqualTo(42));
                return(PcreCalloutResult.Abort);
            });

            Assert.That(match.Success, Is.False);
        }
Example #7
0
        public void should_use_callout_result()
        {
            var regex = new PcreRegex(@"(\d+)(*SKIP)(?C1):\s*(\w+)");

            var match = regex.Match(
                "1542: not_this, 1764: hello",
                data => data.Number == 1 &&
                int.Parse(data.Match[1].Value) % 42 == 0
                    ? PcreCalloutResult.Pass
                    : PcreCalloutResult.Fail);

            Assert.That(match[2].Value, Is.EqualTo("hello"));
        }
Example #8
0
        public void should_match_partially(PcreMatchOptions options)
        {
            var re = new PcreRegex(@"(?<=abc)123");

            var match = re.Match("xyzabc12", options);

            Assert.That(match.Success, Is.False);
            Assert.That(match.IsPartialMatch, Is.True);
            Assert.That(match.Index, Is.EqualTo(6));
            Assert.That(match.EndIndex, Is.EqualTo(8));
            Assert.That(match.Length, Is.EqualTo(2));
            Assert.That(match.Value, Is.EqualTo("12"));
        }
Example #9
0
        public void should_match_script_run()
        {
            const string subject = "123\U0001D7CF\U0001D7D0\U0001D7D1";

            var normal = PcreRegex.Match(subject, @"\d+", PcreOptions.Unicode);

            Assert.That(normal.Success, Is.True);
            Assert.That(normal.Index, Is.EqualTo(0));
            Assert.That(normal.Length, Is.EqualTo(subject.Length));

            var scriptRun = PcreRegex.Match(subject, @"(*script_run:\d+)", PcreOptions.Unicode);

            Assert.That(scriptRun.Success, Is.True);
            Assert.That(scriptRun.Index, Is.EqualTo(0));
            Assert.That(scriptRun.Length, Is.EqualTo(3));
        }
Example #10
0
        public void should_auto_callout_ref()
        {
            var re = new PcreRegex(@"a.c", PcreOptions.AutoCallout);

            var count = 0;

            var match = re.Match("abc".AsSpan(), data =>
            {
                Assert.That(data.Number, Is.EqualTo(255));
                ++count;
                return(PcreCalloutResult.Pass);
            });

            Assert.That(match.Success, Is.True);
            Assert.That(count, Is.EqualTo(4));
        }
Example #11
0
        public void should_provide_callout_flags()
        {
            var re = new PcreRegex(@"a(?C1)(?:(?C2)(*FAIL)|b)(?C3)");

            var startMatchList = new List <bool>();
            var backtrackList  = new List <bool>();

            var match = re.Match("abc", data =>
            {
                startMatchList.Add(data.StartMatch);
                backtrackList.Add(data.Backtrack);
                return(PcreCalloutResult.Pass);
            });

            Assert.That(startMatchList, Is.EqualTo(new[] { true, false, false }));
            Assert.That(backtrackList, Is.EqualTo(new[] { false, false, true }));
        }
Example #12
0
        public void should_handle_case_sensitive_group_names_ref()
        {
            var re = new PcreRegex(@"a+(?<grp>b+)(?<GRP>c+)(?<GrP>d+)e+");

            var match = re.Match("xxxaaabbcccddeeezzz".AsSpan());

            Assert.That(match["grp"].Value.ToString(), Is.EqualTo("bb"));
            Assert.That(match["grp"].Index, Is.EqualTo(6));
            Assert.That(match["grp"].Length, Is.EqualTo(2));

            Assert.That(match["GRP"].Value.ToString(), Is.EqualTo("ccc"));
            Assert.That(match["GRP"].Index, Is.EqualTo(8));
            Assert.That(match["GRP"].Length, Is.EqualTo(3));

            Assert.That(match["GrP"].Value.ToString(), Is.EqualTo("dd"));
            Assert.That(match["GrP"].Index, Is.EqualTo(11));
            Assert.That(match["GrP"].Length, Is.EqualTo(2));
        }
Example #13
0
        public void should_match_pattern_ref()
        {
            var re    = new PcreRegex(@"a+(b+)c+");
            var match = re.Match("xxxaaabbccczzz".AsSpan());

            Assert.That(match.Success, Is.True);
            Assert.That(match.CaptureCount, Is.EqualTo(1));
            Assert.That(match.Value.ToString(), Is.EqualTo("aaabbccc"));
            Assert.That(match.Index, Is.EqualTo(3));
            Assert.That(match.EndIndex, Is.EqualTo(11));
            Assert.That(match.Length, Is.EqualTo(8));

            Assert.That(match[1].Success, Is.True);
            Assert.That(match[1].Value.ToString(), Is.EqualTo("bb"));
            Assert.That(match[1].Index, Is.EqualTo(6));
            Assert.That(match[1].Length, Is.EqualTo(2));

            Assert.That(match[2].Success, Is.False);
        }
Example #14
0
        public void should_match_pattern()
        {
            var re    = new PcreRegex(@"a+(b+)c+");
            var match = re.Match("xxxaaabbccczzz");

            Assert.That(match, Is.Not.Null);
            Assert.That(match.Success, Is.True);
            Assert.That(match.CaptureCount, Is.EqualTo(1));
            Assert.That(match.Value, Is.EqualTo("aaabbccc"));
            Assert.That(match.Index, Is.EqualTo(3));
            Assert.That(match.EndIndex, Is.EqualTo(11));
            Assert.That(match.Length, Is.EqualTo(8));

            Assert.That(match[1], Is.Not.Null);
            Assert.That(match[1].Value, Is.EqualTo("bb"));
            Assert.That(match[1].Index, Is.EqualTo(6));
            Assert.That(match[1].Length, Is.EqualTo(2));

            Assert.That(match.Groups[1], Is.SameAs(match[1]));
        }
Example #15
0
        public void should_execute_failing_callout()
        {
            var re = new PcreRegex(@".(?C42)");

            var first = true;

            var match = re.Match("ab", data =>
            {
                Assert.That(data.Number, Is.EqualTo(42));
                if (first)
                {
                    first = false;
                    return(PcreCalloutResult.Fail);
                }
                return(PcreCalloutResult.Pass);
            });

            Assert.That(match, Is.Not.Null);
            Assert.That(match.Success, Is.True);
            Assert.That(match.Value, Is.EqualTo("b"));
        }
Example #16
0
        public void should_execute_string_callout()
        {
            const string pattern = @"(a)(*MARK:foo)(x)?(?C{bar})(bc)";
            var          re      = new PcreRegex(pattern);

            var calls = 0;

            var match = re.Match("abc", data =>
            {
                Assert.That(data.Number, Is.EqualTo(0));
                Assert.That(data.CurrentOffset, Is.EqualTo(1));
                Assert.That(data.PatternPosition, Is.EqualTo(pattern.IndexOf("(?C{bar})", StringComparison.Ordinal) + 9));
                Assert.That(data.StartOffset, Is.EqualTo(0));
                Assert.That(data.LastCapture, Is.EqualTo(1));
                Assert.That(data.MaxCapture, Is.EqualTo(2));
                Assert.That(data.NextPatternItemLength, Is.EqualTo(4));
                Assert.That(data.StringOffset, Is.EqualTo(pattern.IndexOf("(?C{bar})", StringComparison.Ordinal) + 4));
                Assert.That(data.String, Is.EqualTo("bar"));

                Assert.That(data.Match.Value, Is.EqualTo("a"));
                Assert.That(data.Match[1].Value, Is.EqualTo("a"));
                Assert.That(data.Match[2].Success, Is.False);
                Assert.That(data.Match[2].Value, Is.SameAs(string.Empty));
                Assert.That(data.Match[3].Success, Is.False);
                Assert.That(data.Match[3].Value, Is.SameAs(string.Empty));

                Assert.That(data.Match.Mark, Is.EqualTo("foo"));

                ++calls;
                return(PcreCalloutResult.Pass);
            });

            Assert.That(match, Is.Not.Null);
            Assert.That(match.Success, Is.True);
            Assert.That(calls, Is.EqualTo(1));
        }
        public bool Match(string input, string pattern)
        {
            var match = PcreRegex.Match(input, pattern);

            return(match.Success);
        }
 public PcreMatch PcreRegexCompiled() => pcreregexCompiled.Match(Source);
 public PcreMatch PcreRegex() => pcreregex.Match(Source);
Example #20
0
 public bool Progress(string s)
 {
     return(Regex.Match(s, PcreMatchOptions.PartialSoft).IsPartialMatch || Accepted(s));
 }
Example #21
0
 public void PcreRegex_Good() => pcreregex.Match(goodGibberish);
Example #22
0
 public void PcreRegexCompiled() => pcreregexCompiled.Match(Source);
Example #23
0
 public void PcreRegex() => pcreregex.Match(Source);