예제 #1
0
        private static void VerifyMatch(Match match, CaptureData expected)
        {
            Assert.True(match.Success);
            Assert.Equal(expected.Index, match.Index);
            Assert.Equal(expected.Length, match.Length);
            RegexAssert.Equal(expected.Value, match);

            Assert.Equal(expected.Index, match.Groups[0].Index);
            Assert.Equal(expected.Length, match.Groups[0].Length);
            RegexAssert.Equal(expected.Value, match.Groups[0]);

            Assert.Equal(1, match.Captures.Count);
            Assert.Equal(expected.Index, match.Captures[0].Index);
            Assert.Equal(expected.Length, match.Captures[0].Length);
            RegexAssert.Equal(expected.Value, match.Captures[0]);
        }
예제 #2
0
        public async Task Matches_MultipleCapturingGroups(RegexEngine engine)
        {
            string[] expectedGroupValues        = { "abracadabra", "abra", "cad" };
            string[] expectedGroupCaptureValues = { "abracad", "abra" };

            // Another example - given by Brad Merril in an article on RegularExpressions
            Regex regex = await RegexHelpers.GetRegexAsync(engine, @"(abra(cad)?)+");

            string input = "abracadabra1abracadabra2abracadabra3";
            Match  match = regex.Match(input);

            while (match.Success)
            {
                string expected = "abracadabra";
                RegexAssert.Equal(expected, match);
                if (!RegexHelpers.IsNonBacktracking(engine))
                {
                    Assert.Equal(3, match.Groups.Count);
                    for (int i = 0; i < match.Groups.Count; i++)
                    {
                        RegexAssert.Equal(expectedGroupValues[i], match.Groups[i]);
                        if (i == 1)
                        {
                            Assert.Equal(2, match.Groups[i].Captures.Count);
                            for (int j = 0; j < match.Groups[i].Captures.Count; j++)
                            {
                                RegexAssert.Equal(expectedGroupCaptureValues[j], match.Groups[i].Captures[j]);
                            }
                        }
                        else if (i == 2)
                        {
                            Assert.Equal(1, match.Groups[i].Captures.Count);
                            RegexAssert.Equal("cad", match.Groups[i].Captures[0]);
                        }
                    }
                    Assert.Equal(1, match.Captures.Count);
                    RegexAssert.Equal("abracadabra", match.Captures[0]);
                }
                match = match.NextMatch();
            }
        }
예제 #3
0
        public void Matches_MultipleCapturingGroups()
        {
            string[] expectedGroupValues        = { "abracadabra", "abra", "cad" };
            string[] expectedGroupCaptureValues = { "abracad", "abra" };

            // Another example - given by Brad Merril in an article on RegularExpressions
            Regex  regex = new Regex(@"(abra(cad)?)+");
            string input = "abracadabra1abracadabra2abracadabra3";
            Match  match = regex.Match(input);

            while (match.Success)
            {
                string expected = "abracadabra";
                RegexAssert.Equal(expected, match);

                Assert.Equal(3, match.Groups.Count);
                for (int i = 0; i < match.Groups.Count; i++)
                {
                    RegexAssert.Equal(expectedGroupValues[i], match.Groups[i]);
                    if (i == 1)
                    {
                        Assert.Equal(2, match.Groups[i].Captures.Count);
                        for (int j = 0; j < match.Groups[i].Captures.Count; j++)
                        {
                            RegexAssert.Equal(expectedGroupCaptureValues[j], match.Groups[i].Captures[j]);
                        }
                    }
                    else if (i == 2)
                    {
                        Assert.Equal(1, match.Groups[i].Captures.Count);
                        RegexAssert.Equal("cad", match.Groups[i].Captures[0]);
                    }
                }
                Assert.Equal(1, match.Captures.Count);
                RegexAssert.Equal("abracadabra", match.Captures[0]);
                match = match.NextMatch();
            }
        }