Пример #1
0
        public void ORegex_InvalidRegularExpression()
        {
            var invalidRegex = "(.*";
            var r            = new ORegex(invalidRegex);

            Assert.IsFalse(r.Valid);

            try
            {
                r.SafeSearch("test");
                Assert.Fail("An invalid regular expression did not throw an ArgumentException");
            }
            catch (ArgumentException) { }

            try
            {
                r.IndexIn("test");
                Assert.Fail("An invalid regular expression did not throw an ArgumentException");
            }
            catch (ArgumentException) { }

            try
            {
                r.Search("test");
                Assert.Fail("An invalid regular expression did not throw an ArgumentException");
            }
            catch (ArgumentException) { }
        }
Пример #2
0
        private void TestRegexEqualityButton_Click(object sender, EventArgs e)
        {
            var regex    = new Regex(RegexPatternBox.Text);
            var oregex   = new ORegex <char>(ORegexPatternBox.Text, ORegexOptions.None, _table);
            var matches  = regex.Matches(InputTextBox.Text).Cast <Match>().ToArray();
            var omatches = oregex.Matches(InputTextBox.Text.ToCharArray()).ToArray();

            if (matches.Length != omatches.Length)
            {
                MessageBox.Show("Invalid matches count!", "Count", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            for (int i = 0; i < matches.Length; i++)
            {
                var exp = matches[i];
                var act = omatches[i];

                if (exp.Index != act.Index || exp.Length != act.Length)
                {
                    MessageBox.Show("Invalid range!", "Match dismatch", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            MessageBox.Show("All good.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Пример #3
0
        private void EvaluateLook(
            int start,
            int end,
            FSA <TValue> fsa,
            LookAheadQuantifier quantifier,
            AstConcatNode concatNode,
            ORegexOptions options)
        {
            bool isBehind   = options.HasFlag(ORegexOptions.ReversePattern) ? !quantifier.IsBehind : quantifier.IsBehind;
            bool isNegative = quantifier.IsNegative;

            var condOptions = isBehind ? ORegexOptions.RightToLeft : ORegexOptions.None;
            var concat      = new AstConcatNode(concatNode.Children, concatNode.Range);
            var root        = new AstRootNode(concat,
                                              true,
                                              false,
                                              concat.Range,
                                              new[]
            {
                ORegexAstFactory <TValue> .MainCaptureName
            });
            var fa     = Create(root, condOptions);
            var oregex = new ORegex <TValue>(fa, condOptions);

            var func = new ORegexPredicateEdge <TValue>("#look", oregex, isNegative, isBehind);

            EvaluateCondition(start, end, fsa, func);
        }
Пример #4
0
        public void ORegex_IgnoreCase()
        {
            using (var r = new ORegex("A"))
            {
                Assert.IsTrue(r.Valid);

                r.Search("A");
                Assert.AreEqual(0, r.MatchPosition(0));

                r.Search("a");
                Assert.AreEqual(0, r.MatchPosition(0));
            }

            using (var r = new ORegex("A", true))
            {
                Assert.IsTrue(r.Valid);

                r.Search("A");
                Assert.AreEqual(0, r.MatchPosition(0));

                r.Search("a");
                Assert.AreEqual(0, r.MatchPosition(0));
            }

            using (var r = new ORegex("A", false))
            {
                Assert.IsTrue(r.Valid);

                r.Search("A");
                Assert.AreEqual(0, r.MatchPosition(0));

                r.Search("a");
                Assert.AreEqual(-1, r.MatchPosition(0));
            }
        }
Пример #5
0
        public void ORegex_Search_Offset_Capture()
        {
            using (var r = new ORegex("(A)"))
            {
                Assert.IsTrue(r.Valid);
                r.Search("-A-", 1);

                Assert.AreEqual(1, r.MatchPosition(0));
                Assert.AreEqual(1, r.MatchLength(0));
                Assert.AreEqual("A", r.Capture(0));

                Assert.AreEqual(1, r.MatchPosition(1));
                Assert.AreEqual(1, r.MatchLength(1));
                Assert.AreEqual("A", r.Capture(1));

                Assert.AreEqual(-1, r.MatchPosition(2));
                Assert.AreEqual(-1, r.MatchLength(2));
                Assert.AreEqual(null, r.Capture(2));

                r.Search("--A", 1);

                Assert.AreEqual(2, r.MatchPosition(0));
                Assert.AreEqual(1, r.MatchLength(0));
                Assert.AreEqual("A", r.Capture(0));

                Assert.AreEqual(2, r.MatchPosition(1));
                Assert.AreEqual(1, r.MatchLength(1));
                Assert.AreEqual("A", r.Capture(1));

                Assert.AreEqual(-1, r.MatchPosition(2));
                Assert.AreEqual(-1, r.MatchLength(2));
                Assert.AreEqual(null, r.Capture(2));
            }
        }
Пример #6
0
        public void ORegex_ObjectDisposedExceptions()
        {
            var r = new ORegex("A");

            r.Dispose();

            try
            {
                r.SafeSearch("test");
                Assert.Fail();
            }
            catch (ObjectDisposedException) { }

            try
            {
                r.Search("test");
                Assert.Fail();
            }
            catch (ObjectDisposedException) { }

            try
            {
                r.IndexIn("test");
                Assert.Fail();
            }
            catch (ObjectDisposedException) { }

            try
            {
                r.MatchPosition(0);
                Assert.Fail();
            }
            catch (ObjectDisposedException) { }

            try
            {
                r.MatchLength(0);
                Assert.Fail();
            }
            catch (ObjectDisposedException) { }

            try
            {
                r.Capture(0);
                Assert.Fail();
            }
            catch (ObjectDisposedException) { }

            // IDisposable.Dispose() should be idempotent
            try
            {
                r.Dispose();
            }
            catch (ObjectDisposedException)
            {
                Assert.Fail();
            }
        }
Пример #7
0
        public void NumberGroupTest()
        {
            var oregex = new ORegex<int>("((?<pair>{0}){1})+", x => x != 0, x => x == 0);

            var input = new[] { 0, 0, 0, 1, 0, 2, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0 };
            foreach (var match in oregex.Matches(input))
            {
                Trace.WriteLine(string.Join(",", match.Values));
            }
        }
Пример #8
0
        public void NumberGroupTest()
        {
            var oregex = new ORegex <int>("((?<pair>{0}){1})+", x => x != 0, x => x == 0);

            var input = new[] { 0, 0, 0, 1, 0, 2, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0 };

            foreach (var match in oregex.Matches(input))
            {
                Trace.WriteLine(string.Join(",", match.Values));
            }
        }
Пример #9
0
 public void ORegex_IndexIn_WithOffset()
 {
     using (var r = new ORegex("A"))
     {
         Assert.IsTrue(r.Valid);
         Assert.AreEqual(-1, r.IndexIn("A--", 1));
         Assert.AreEqual(1, r.IndexIn("AA-", 1));
         Assert.AreEqual(2, r.IndexIn("A-A", 2));
         Assert.AreEqual(2, r.IndexIn("--A", 2));
     }
 }
Пример #10
0
        /// <summary>
        /// This is not intended as a full test suite. It's just here to easily
        /// test that the dll/so/dylib works on a given environment.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            var text    = "abcABC123";
            var pattern = "[A-C]+";

            Console.WriteLine("Building ORegex({0})", text);

            using (var re = new ORegex(pattern, false))
            {
                Console.WriteLine("Looking for {0} in {1}", pattern, text);
                Console.WriteLine("Found a match at {0}", re.IndexIn(text));
            }
        }
Пример #11
0
        public void ORegex_NoCaptureExceptions()
        {
            using (var r = new ORegex("A"))
            {
                // No region is defined since we haven't searched
                try
                {
                    r.MatchPosition(0);
                    Assert.Fail();
                }
                catch (InvalidOperationException) { }

                try
                {
                    r.MatchLength(0);
                    Assert.Fail();
                }
                catch (InvalidOperationException) { }

                try
                {
                    r.Capture(0);
                    Assert.Fail();
                }
                catch (InvalidOperationException) { }

                // IndexIn should not create a region. Only Search does that.
                r.IndexIn("A");
                try
                {
                    r.MatchPosition(0);
                    Assert.Fail();
                }
                catch (InvalidOperationException) { }

                try
                {
                    r.MatchLength(0);
                    Assert.Fail();
                }
                catch (InvalidOperationException) { }

                try
                {
                    r.Capture(0);
                    Assert.Fail();
                }
                catch (InvalidOperationException) { }
            }
        }
Пример #12
0
        public void ORegex_MultilineSearch()
        {
            using (var r = new ORegex("A.B", multiline: false))
            {
                Assert.IsTrue(r.Valid);
                Assert.AreEqual(-1, r.IndexIn("A\nB"));
            }

            using (var r = new ORegex("A.B", multiline: true))
            {
                Assert.IsTrue(r.Valid);
                Assert.AreEqual(0, r.IndexIn("A\nB"));
            }
        }
Пример #13
0
        public void PrimeTest()
        {
            var oregex = new ORegex<int>("{0}(?<pair>.{0})*", IsPrime);

            var input = new[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
            foreach (var match in oregex.Matches(input))
            {
                Trace.WriteLine(string.Join(",", match.Values));
            }
            //OUTPUT:
            //2
            //3,4,5,6,7
            //11,12,13
        }
Пример #14
0
        public void ORegex_SafeSearch_SparseMatches()
        {
            using (var r = new ORegex("((A)|(B))(.*)"))
            {
                Assert.IsTrue(r.Valid);
                var result = r.SafeSearch("---A---");
                Assert.AreEqual(result.Count, 5);

                Assert.AreEqual(-1, result[3].Position);
                Assert.AreEqual(0, result[3].Length);

                Assert.AreEqual(4, result[4].Position);
                Assert.AreEqual(3, result[4].Length);
            }
        }
Пример #15
0
        public void PrimeTest()
        {
            var oregex = new ORegex <int>("{0}(?<pair>.{0})*", IsPrime);

            var input = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };

            foreach (var match in oregex.Matches(input))
            {
                Trace.WriteLine(string.Join(",", match.Values));
            }
            //OUTPUT:
            //2
            //3,4,5,6,7
            //11,12,13
        }
Пример #16
0
        public void ORegex_SafeSearch()
        {
            using (var r = new ORegex("(A)(.*)"))
            {
                Assert.IsTrue(r.Valid);
                var result = r.SafeSearch("---A---");
                Assert.AreEqual(result.Count, 3);

                Assert.AreEqual(3, result[0].Position);
                Assert.AreEqual(4, result[0].Length);

                Assert.AreEqual(3, result[1].Position);
                Assert.AreEqual(1, result[1].Length);

                Assert.AreEqual(4, result[2].Position);
                Assert.AreEqual(3, result[2].Length);
            }
        }
Пример #17
0
        public void ORegex_Search()
        {
            using (var r = new ORegex("A"))
            {
                Assert.IsTrue(r.Valid);
                Assert.IsFalse(r.Ran);
                r.Search("-A-");
                Assert.IsTrue(r.Ran);

                Assert.AreEqual(1, r.MatchPosition(0));
                Assert.AreEqual(1, r.MatchLength(0));
                Assert.AreEqual("A", r.Capture(0));

                Assert.AreEqual(-1, r.MatchPosition(1));
                Assert.AreEqual(-1, r.MatchLength(1));
                Assert.AreEqual(null, r.Capture(1));
            }
        }
Пример #18
0
        public void PersonSelectionTest()
        {
            //INPUT_TEXT: Пяточкова Тамара решила выгулять Джека и встретилась с Михаилом А.М.
            var sentence = new[]
            {
                new Word("Пяточкова", SemanticType.FamilyName),
                new Word("Тамара", SemanticType.Name),
                new Word("решила", SemanticType.Other),
                new Word("выгулять", SemanticType.Other),
                new Word("Джека", SemanticType.Name),
                new Word("и", SemanticType.Other),
                new Word("встретилась", SemanticType.Other),
                new Word("с", SemanticType.Other),
                new Word("Михаилом", SemanticType.Name),
                new Word("А.", SemanticType.Other),
                new Word("М", SemanticType.Other),
            };

            //Creating table which will contain our predicates.
            var pTable = new PredicateTable <Word>();

            pTable.AddPredicate("Фамилия", x => x.SemType == SemanticType.FamilyName);  //Check if word is FamilyName.
            pTable.AddPredicate("Имя", x => x.SemType == SemanticType.Name);            //Check if word is simple Name.
            pTable.AddPredicate("Инициал", x => IsInitial(x.Value));                    //Complex check if Value is Inital character.

            var oregex = new ORegex <Word>(@"
                {Фамилия}(?<name>{Имя})                     //Comments can written inside pattern...
                |
                (?<name>{Имя})({Фамилия}|{Инициал}{1,2})?  /*...even complex ones.*/
            ", pTable);

            var persons = oregex.Matches(sentence).Select(x => new Person(x)).ToArray();

            foreach (var person in persons)
            {
                Console.WriteLine("Person found: {0}, length: {1}", person.Name, person.Words.Length);
            }
            Assert.AreEqual(persons.Length, 3);
            //OUTPUT:
            //Person found: Тамара, length: 2
            //Person found: Джека, length: 1
            //Person found: Михаилом, length: 3
        }
Пример #19
0
        public void PersonSelectionTest()
        {
            //INPUT_TEXT: Пяточкова Тамара решила выгулять Джека и встретилась с Михаилом А.М.
            var sentence = new[]
            {
                new Word("Пяточкова", SemanticType.FamilyName),
                new Word("Тамара", SemanticType.Name),
                new Word("решила", SemanticType.Other),
                new Word("выгулять", SemanticType.Other),
                new Word("Джека", SemanticType.Name),
                new Word("и", SemanticType.Other),
                new Word("встретилась", SemanticType.Other),
                new Word("с", SemanticType.Other),
                new Word("Михаилом", SemanticType.Name),
                new Word("А.", SemanticType.Other),
                new Word("М", SemanticType.Other),
            };

            //Creating table which will contain our predicates.
            var pTable = new PredicateTable<Word>();
            pTable.AddPredicate("Фамилия", x => x.SemType == SemanticType.FamilyName);  //Check if word is FamilyName.
            pTable.AddPredicate("Имя", x => x.SemType == SemanticType.Name);            //Check if word is simple Name.
            pTable.AddPredicate("Инициал", x => IsInitial(x.Value));                    //Complex check if Value is Inital character.

            var oregex = new ORegex<Word>(@"
                {Фамилия}(?<name>{Имя})                     //Comments can written inside pattern...
                |
                (?<name>{Имя})({Фамилия}|{Инициал}{1,2})?  /*...even complex ones.*/
            ", pTable);

            var persons = oregex.Matches(sentence).Select(x => new Person(x)).ToArray();

            foreach (var person in persons)
            {
                Console.WriteLine("Person found: {0}, length: {1}", person.Name, person.Words.Length);
            }
            Assert.AreEqual(persons.Length, 3);
            //OUTPUT:
            //Person found: Тамара, length: 2
            //Person found: Джека, length: 1
            //Person found: Михаилом, length: 3
        }
Пример #20
0
        public void ByteMaskSearchTest()
        {
            var oregex = new ORegex <byte>("{0}{1}{2}{3}{4}{5}{6}{7}", 12, 3, 5, 76, 8, 0, 6, 125);

            byte[] toBeSearched = new byte[]
            {
                23, 36, 43, 76, 125, 56, 34, 234, 12, 3, 5, 76, 8, 0, 6, 125, 234, 56, 211, 122, 22, 4, 7, 89, 76, 64, 12,
                3, 5, 76, 8, 0, 6, 125
            };

            var matches = oregex.Matches(toBeSearched);

            Assert.AreEqual(matches.Count, 2);
            foreach (var m in matches)
            {
                Console.WriteLine("Match at {0} found:\t{1}", m.Index, string.Join(",", m.Values));
            }
            //OUTPUT:
            //Match at 8 found:     12,3,5,76,8,0,6,125
            //Match at 26 found:    12,3,5,76,8,0,6,125
        }
Пример #21
0
        public void ORegex_Search_MultipleCaptures()
        {
            using (var r = new ORegex("(A)(.*)"))
            {
                Assert.IsTrue(r.Valid);
                r.Search("---A---");
                Assert.AreEqual(3, r.MatchPosition(0));
                Assert.AreEqual(4, r.MatchLength(0));
                Assert.AreEqual("A---", r.Capture(0));

                Assert.AreEqual(3, r.MatchPosition(1));
                Assert.AreEqual(1, r.MatchLength(1));
                Assert.AreEqual("A", r.Capture(1));

                Assert.AreEqual(4, r.MatchPosition(2));
                Assert.AreEqual(3, r.MatchLength(2));
                Assert.AreEqual("---", r.Capture(2));

                Assert.AreEqual(-1, r.MatchPosition(3));
                Assert.AreEqual(-1, r.MatchLength(3));
                Assert.AreEqual(null, r.Capture(3));
            }
        }
Пример #22
0
        private void TestRegexEqualityButton_Click(object sender, EventArgs e)
        {
            var regex = new Regex(RegexPatternBox.Text);
            var oregex = new ORegex<char>(ORegexPatternBox.Text, ORegexOptions.None, _table);
            var matches = regex.Matches(InputTextBox.Text).Cast<Match>().ToArray();
            var omatches = oregex.Matches(InputTextBox.Text.ToCharArray()).ToArray();

            if (matches.Length != omatches.Length)
            {
                MessageBox.Show("Invalid matches count!", "Count", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            for (int i = 0; i < matches.Length; i++)
            {
                var exp = matches[i];
                var act = omatches[i];

                if (exp.Index != act.Index || exp.Length != act.Length)
                {
                    MessageBox.Show("Invalid range!", "Match dismatch", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            MessageBox.Show("All good.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Пример #23
0
        public void ByteMaskSearchTest()
        {
            var oregex = new ORegex<byte>("{0}{1}{2}{3}{4}{5}{6}{7}", 12, 3, 5, 76, 8, 0, 6, 125);
            byte[] toBeSearched = new byte[]
            {
                23, 36, 43, 76, 125, 56, 34, 234, 12, 3, 5, 76, 8, 0, 6, 125, 234, 56, 211, 122, 22, 4, 7, 89, 76, 64, 12,
                3, 5, 76, 8, 0, 6, 125
            };

            var matches = oregex.Matches(toBeSearched);
            Assert.AreEqual(matches.Count, 2);
            foreach (var m in matches)
            {
                Console.WriteLine("Match at {0} found:\t{1}",m.Index,string.Join(",", m.Values));
            }
            //OUTPUT:
            //Match at 8 found:     12,3,5,76,8,0,6,125
            //Match at 26 found:    12,3,5,76,8,0,6,125
        }
Пример #24
0
 public ORegexPredicateEdge(string name, ORegex <TValue> oregex, bool isNegative, bool isBehind) : base(name, true)
 {
     _isNegative = isNegative;
     _oregex     = oregex;
     _isBehind   = isBehind;
 }