Example #1
0
        private static Func <PcreCallout, PcreCalloutResult> AutoCalloutHandler(string text)
        {
            return(arg =>
            {
                var patternStart = Math.Max(0, arg.PatternPosition - 20);
                var patternEnd = Math.Min(PATTERN.Length - 1, arg.PatternPosition + arg.NextPatternItemLength + 20);
                var pattern = (
                    PATTERN.Substring(patternStart, arg.PatternPosition - patternStart)
                    + HILITE + PATTERN.Substring(arg.PatternPosition, arg.NextPatternItemLength) + NORMAL
                    + PATTERN.Substring(
                        arg.PatternPosition + arg.NextPatternItemLength,
                        patternEnd - (arg.PatternPosition + arg.NextPatternItemLength - 1)
                        )
                    );

                var textStart = Math.Max(0, arg.Match.Index - 20);
                var textEnd = Math.Min(text.Length - 1, arg.Match.EndIndex + 20);
                var at = (
                    text.Substring(textStart, arg.Match.Index - textStart)
                    + HILITE + arg.Match.Value + NORMAL
                    + text.Substring(arg.Match.EndIndex, textEnd - arg.Match.EndIndex)
                    ).Replace("\t", "\\t").Replace("\n", "\\n").Replace("\r", "\\r");

                Console.WriteLine(DebugRE.Replace(pattern, ""));
#if !ANSI_TERMINAL
                Console.WriteLine(
                    new string(' ', DebugRE.Replace(PATTERN.Substring(patternStart, arg.PatternPosition - patternStart), "").Length)
                    + new string('^', DebugRE.Replace(PATTERN.Substring(arg.PatternPosition, arg.NextPatternItemLength), "").Length)
                    );
#endif

                Console.WriteLine(at);
#if !ANSI_TERMINAL
                Console.WriteLine(
                    new string(' ', text.Substring(textStart, arg.Match.Index - textStart).Replace("\t", "\\t").Replace("\n", "\\n").Replace("\r", "\\r").Length)
                    + new string('^', Math.Max(1, arg.Match.Length))
                    );
#endif
                //Console.WriteLine($"mi:{arg.Match.Index},me:{arg.Match.EndIndex},ml:{arg.Match.Length},cc:{arg.Match.CaptureCount},{arg.Match.Success},{(arg.Match.Success ? $"'{arg.Match.Value}'" : "---")}");

                Console.WriteLine($"co:{arg.CurrentOffset},lc:{arg.LastCapture},xc:{arg.MaxCapture},sm:{arg.StartMatch},so:{arg.StartOffset},'{arg.String}',So:{arg.StringOffset},bt:{arg.Backtrack}");
                Console.WriteLine(string.Join("; ", arg.Match.Groups.Select(g => $"@{g.Index},{g.Success},{(g.Success ? $"'{g.Value}'" : "---")}")));

                Console.WriteLine();

                return PcreCalloutResult.Pass;
            });
        }
Example #2
0
        public void should_replace_indexed_groups_without_braces_at_end_of_pattern()
        {
            var re     = new PcreRegex(@"a+(?<grp>b+)", PcreOptions.IgnoreCase);
            var result = re.Replace("foo aabb bar aaabbab baz", "#$1");

            Assert.That(result, Is.EqualTo("foo #bb bar #bb#b baz"));
        }
Example #3
0
        public void should_insert_nothing_if_there_are_no_groups()
        {
            var re     = new PcreRegex(@"a+");
            var result = re.Replace("foo bar", "<$+>");

            Assert.That(result, Is.EqualTo("foo b<>r"));
        }
Example #4
0
        public void should_replace_matches_with_callback()
        {
            var re     = new PcreRegex(@"a+", PcreOptions.IgnoreCase);
            var result = re.Replace("foo aaa bar aAAa baz", match => match.Length.ToString(CultureInfo.InvariantCulture));

            Assert.That(result, Is.EqualTo("foo 3 b1r 4 b1z"));
        }
Example #5
0
        public void should_insert_last_matched_group()
        {
            var re     = new PcreRegex(@"a+(b+)(c+)?", PcreOptions.IgnoreCase);
            var result = re.Replace("foo aabb bar aaabbabcc baz", "<$+>");

            Assert.That(result, Is.EqualTo("foo <bb> bar <bb><cc> baz"));
        }
Example #6
0
        public void should_insert_nothing_if_no_group_matched()
        {
            var re     = new PcreRegex(@"a+(b+)?");
            var result = re.Replace("foo aabb bar baz", "<$+>");

            Assert.That(result, Is.EqualTo("foo <bb> b<>r b<>z"));
        }
Example #7
0
        public void should_insert_dollar()
        {
            var re     = new PcreRegex(@"a+(b+)", PcreOptions.IgnoreCase);
            var result = re.Replace("foo aabb bar aaabbab baz", "$$-$");

            Assert.That(result, Is.EqualTo("foo $-$ bar $-$$-$ baz"));
        }
Example #8
0
        public void should_insert_subject()
        {
            var re     = new PcreRegex(@"a+(b+)", PcreOptions.IgnoreCase);
            var result = re.Replace("foo aabb bar aaabbab baz", "<$_>");

            Assert.That(result, Is.EqualTo("foo <foo aabb bar aaabbab baz> bar <foo aabb bar aaabbab baz><foo aabb bar aaabbab baz> baz"));
        }
Example #9
0
        public void should_replace_matches_with_pattern()
        {
            var re     = new PcreRegex(@"a+(b+)", PcreOptions.IgnoreCase);
            var result = re.Replace("foo aabb bar aaabbab baz", "<$0><$1><$&>");

            Assert.That(result, Is.EqualTo("foo <aabb><bb><aabb> bar <aaabb><bb><aaabb><ab><b><ab> baz"));
        }
Example #10
0
        public void should_replace_named_groups()
        {
            var re     = new PcreRegex(@"a+(?<grp>b+)", PcreOptions.IgnoreCase);
            var result = re.Replace("foo aabb bar aaabbab baz", "<${grp}>");

            Assert.That(result, Is.EqualTo("foo <bb> bar <bb><b> baz"));
        }
Example #11
0
        public void should_start_at_given_index_and_replace_count()
        {
            var re     = new PcreRegex(@"a+", PcreOptions.IgnoreCase);
            var result = re.Replace("foo aabb bar aaabbab baz", "X", 2, 8);

            Assert.That(result, Is.EqualTo("foo aabb bXr Xbbab baz"));
        }
Example #12
0
        private static JObject ExtractXml(string text, out string error)
        {
            var result = new JObject
            {
                { EXPENSE, new JObject() },
                { "_original", text }
            };

            foreach (PcreMatch match in ExtractXmlRE.Matches(text
#if PCRE_DEBUG
                                                             , 0, AutoCalloutHandler(text)
#endif
                                                             ))
            {
                ProcessXmlFragment(match.Value, result);
            }

            var    expense = (JObject)result[EXPENSE];
            JToken _;
            var    errors =
                from requiredKey in RequiredKeys
                where !expense.TryGetValue(requiredKey, out _)
                select $"Required key '{requiredKey}' was not found!";

            if (LooseXmlRE.IsMatch(ExtractXmlRE.Replace(text, "")))
            {
                errors = errors.Concat(new[] { "Malformed XML found!" });
            }


            error = string.Join(Environment.NewLine, errors).NullIfEmpty();
            return(result);
        }
Example #13
0
        public void should_not_replace_when_count_is_zero()
        {
            var re     = new PcreRegex(@"a+", PcreOptions.IgnoreCase);
            var result = re.Replace("foo aabb bar aaabbab baz", "X", 0);

            Assert.That(result, Is.EqualTo("foo aabb bar aaabbab baz"));
        }
Example #14
0
        public void should_only_replace_given_count()
        {
            var re     = new PcreRegex(@"a+", PcreOptions.IgnoreCase);
            var result = re.Replace("foo aabb bar aaabbab baz", "X", 2);

            Assert.That(result, Is.EqualTo("foo Xbb bXr aaabbab baz"));
        }
Example #15
0
        public void readme_replace_example()
        {
            var result = PcreRegex.Replace("hello, world!!!", @"\p{P}+", "<$&>");

            Assert.That(result, Is.EqualTo("hello<,> world<!!!>"));
        }
Example #16
0
        public string should_handle_backslash_k_in_lookahead(string replacement)
        {
            var re = new PcreRegex(@"(?=a+b\K)");

            return(re.Replace("aabab", replacement));
        }
Example #17
0
        public string should_not_throw_on_invalid_replacement_patterns(string replacement)
        {
            var re = new PcreRegex(@"a+(?<n>b+)", PcreOptions.IgnoreCase);

            return(re.Replace("foo aabb bar aaabbab baz", replacement));
        }
 public string Replace(string input, string pattern, string replacement)
 {
     return(PcreRegex.Replace(input, pattern, replacement));
 }