Example #1
0
        public void should_match_starting_at_end_of_string()
        {
            var re = new PcreRegex(@"(?<=a)");

            Assert.That(re.IsMatch("xxa", 3), Is.True);
            Assert.That(re.IsMatch("xxa".AsSpan(), 3), Is.True);
        }
Example #2
0
        public void should_match_from_index()
        {
            var re = new PcreRegex(@"a");

            Assert.That(re.IsMatch("foobar", 5), Is.False);
            Assert.That(re.IsMatch("foobar".AsSpan(), 5), Is.False);
        }
Example #3
0
        public void readme_json_example()
        {
            const string jsonPattern = @"
                (?(DEFINE)
                    # An object is an unordered set of name/value pairs.
                    (?<object> \{
                        (?: (?&keyvalue) (?: , (?&keyvalue) )* )?
                    (?&ws) \} )
                    (?<keyvalue>
                        (?&ws) (?&string) (?&ws) : (?&value)
                    )

                    # An array is an ordered collection of values.
                    (?<array> \[
                        (?: (?&value) (?: , (?&value) )* )?
                    (?&ws) \] )

                    # A value can be a string in double quotes, or a number,
                    # or true or false or null, or an object or an array.
                    (?<value> (?&ws)
                        (?: (?&string) | (?&number) | (?&object) | (?&array) | true | false | null )
                    )

                    # A string is a sequence of zero or more Unicode characters,
                    # wrapped in double quotes, using backslash escapes.
                    (?<string>
                        "" (?: [^""\\\p{Cc}]++ | \\u[0-9A-Fa-f]{4} | \\ [""\\/bfnrt] )* ""
                        # \p{Cc} matches control characters
                    )

                    # A number is very much like a C or Java number, except that the octal
                    # and hexadecimal formats are not used.
                    (?<number>
                        -? (?: 0 | [1-9][0-9]* ) (?: \. [0-9]+ )? (?: [Ee] [-+]? [0-9]+ )?
                    )

                    # Whitespace
                    (?<ws> \s*+ )
                )

                \A (?&ws) (?&object) (?&ws) \z
            ";

            var regex = new PcreRegex(jsonPattern, PcreOptions.IgnorePatternWhitespace | PcreOptions.Compiled);

            const string subject = @"{
                ""hello"": ""world"",
                ""numbers"": [4, 8, 15, 16, 23, 42],
                ""foo"": null,
                ""bar"": -2.42e+17,
                ""baz"": true
            }";

            Assert.That(regex.IsMatch(subject), Is.True);
            Assert.That(regex.IsMatch(subject.AsSpan()), Is.True);
        }
Example #4
0
        public void should_handle_javascript()
        {
            var re = new PcreRegex(@"^\U$", PcreOptions.JavaScript);

            Assert.That(re.IsMatch("U"), Is.True);
            Assert.That(re.IsMatch("U".AsSpan()), Is.True);

            // ReSharper disable once ObjectCreationAsStatement
            Assert.Throws <ArgumentException>(() => new PcreRegex(@"^\U$"));
        }
Example #5
0
        public void should_handle_extra_options()
        {
            var re = new PcreRegex(@"bar", new PcreRegexSettings(PcreOptions.Literal)
            {
                ExtraCompileOptions = PcreExtraCompileOptions.MatchWord
            });

            Assert.That(re.IsMatch("foo bar baz"), Is.True);
            Assert.That(re.IsMatch("foobar baz"), Is.False);
        }
Example #6
0
        public void should_handle_singleline()
        {
            var re = new PcreRegex("^a.*b$");

            Assert.That(re.IsMatch("a\r\nb"), Is.False);
            Assert.That(re.IsMatch("a\r\nb".AsSpan()), Is.False);

            re = new PcreRegex("^a.*b$", PcreOptions.Singleline);
            Assert.That(re.IsMatch("a\r\nb"), Is.True);
            Assert.That(re.IsMatch("a\r\nb".AsSpan()), Is.True);
        }
Example #7
0
        public void should_handle_ignore_whitespace()
        {
            var re = new PcreRegex("^a b$");

            Assert.That(re.IsMatch("ab"), Is.False);
            Assert.That(re.IsMatch("ab".AsSpan()), Is.False);

            re = new PcreRegex("^a b$", PcreOptions.IgnorePatternWhitespace);
            Assert.That(re.IsMatch("ab"), Is.True);
            Assert.That(re.IsMatch("ab".AsSpan()), Is.True);
        }
Example #8
0
        public void should_handle_ignore_case()
        {
            var re = new PcreRegex("aBc");

            Assert.That(re.IsMatch("Abc"), Is.False);
            Assert.That(re.IsMatch("Abc".AsSpan()), Is.False);

            re = new PcreRegex("aBc", PcreOptions.IgnoreCase);
            Assert.That(re.IsMatch("Abc"), Is.True);
            Assert.That(re.IsMatch("Abc".AsSpan()), Is.True);
        }
Example #9
0
        public void should_handle_unicode_character_properties()
        {
            var re = new PcreRegex(@"^\w$");

            Assert.That(re.IsMatch("à"), Is.False);
            Assert.That(re.IsMatch("à".AsSpan()), Is.False);

            re = new PcreRegex(@"^\w$", PcreOptions.Unicode);
            Assert.That(re.IsMatch("à"), Is.True);
            Assert.That(re.IsMatch("à".AsSpan()), Is.True);
        }
Example #10
0
        public void should_handle_multiline()
        {
            var re = new PcreRegex("^aaa$");

            Assert.That(re.IsMatch("aaa\r\nbbb"), Is.False);
            Assert.That(re.IsMatch("aaa\r\nbbb".AsSpan()), Is.False);

            re = new PcreRegex("^aaa$", PcreOptions.MultiLine);
            Assert.That(re.IsMatch("aaa\r\nbbb"), Is.True);
            Assert.That(re.IsMatch("aaa\r\nbbb".AsSpan()), Is.True);
        }
Example #11
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 #12
0
        public void should_handle_javascript()
        {
            var re = new PcreRegex(@"^\U$", PcreOptions.JavaScript);

            Assert.That(re.IsMatch("U"), Is.True);

            Assert.Throws <ArgumentException>(() => new PcreRegex(@"^\U$"));
        }
Example #13
0
        public bool CheckStartWithA(string name)
        {
            var pcre = new PcreRegex("^A.*");

            return(pcre.IsMatch(name));
        }
Example #14
0
        /// <summary>
        /// 檢查字串是否符合表示式
        /// </summary>
        /// <param name="str">字串實例</param>
        /// <param name="pattern">正規表示式</param>
        /// <returns>是否符合表示式</returns>
        public static bool IsMatch(string str, string pattern)
        {
            var regex = new PcreRegex(pattern);

            return(regex.IsMatch(str));
        }