当前位置: 首页>>代码示例>>C#>>正文


C# Regex.GetGroupNames方法代码示例

本文整理汇总了C#中Regex.GetGroupNames方法的典型用法代码示例。如果您正苦于以下问题:C# Regex.GetGroupNames方法的具体用法?C# Regex.GetGroupNames怎么用?C# Regex.GetGroupNames使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Regex的用法示例。


在下文中一共展示了Regex.GetGroupNames方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: R2LGetGroupNamesMatch

    public static void R2LGetGroupNamesMatch()
    {
        Regex regex = new Regex("(?<first_name>\\S+)\\s(?<last_name>\\S+)");
        Assert.Equal(new string[] { "0", "first_name", "last_name" }, regex.GetGroupNames());

        Match match = regex.Match("David Bau");
        Assert.True(match.Success);
        
        regex = new Regex(@"\D+");
        Assert.False(regex.IsMatch("12321"));
    }
开发者ID:eerhardt,项目名称:corefx,代码行数:11,代码来源:R2LGetGroupNamesMatchTests.cs

示例2: VerifyGroupNames

    public static bool VerifyGroupNames(Regex r, String[] expectedNames, int[] expectedNumbers)
    {
        string[] names = r.GetGroupNames();
        if (names.Length != expectedNames.Length)
        {
            Console.WriteLine("Err_08722aswa! Expect {0} names actual={1}", expectedNames.Length, names.Length);
            return false;
        }

        for (int i = 0; i < expectedNames.Length; i++)
        {
            if (!names[i].Equals(expectedNames[i]))
            {
                Console.WriteLine("Err_09878asfas! Expected GroupNames[{0}]={1} actual={2}", i, expectedNames[i], names[i]);
                return false;
            }

            if (expectedNames[i] != r.GroupNameFromNumber(expectedNumbers[i]))
            {
                Console.WriteLine("Err_6589sdafn!GroupNameFromNumber({0})={1} actual={2}", expectedNumbers[i], expectedNames[i], r.GroupNameFromNumber(expectedNumbers[i]));
                return false;
            }
        }

        return true;
    }
开发者ID:er0dr1guez,项目名称:corefx,代码行数:26,代码来源:GroupNamesAndNumbers.cs

示例3: Groups

        public void Groups(string pattern, string input, RegexOptions options, CultureInfo cultureInfo, string[] expectedGroups)
        {
            CultureInfo originalCulture = CultureInfo.CurrentCulture;
            try
            {
                // In invariant culture, the unicode char matches differ from expected values provided.
                if (originalCulture.Equals(CultureInfo.InvariantCulture))
                {
                    CultureInfo.CurrentCulture = s_enUSCulture;
                }
                if (cultureInfo != null)
                {
                    CultureInfo.CurrentCulture = cultureInfo;
                }
                Regex regex = new Regex(pattern, options);
                Match match = regex.Match(input);
                Assert.True(match.Success);

                Assert.Equal(expectedGroups.Length, match.Groups.Count);
                Assert.True(expectedGroups[0] == match.Value, string.Format("Culture used: {0}", CultureInfo.CurrentCulture));

                int[] groupNumbers = regex.GetGroupNumbers();
                string[] groupNames = regex.GetGroupNames();
                for (int i = 0; i < expectedGroups.Length; i++)
                {
                    Assert.Equal(expectedGroups[i], match.Groups[groupNumbers[i]].Value);
                    Assert.Equal(match.Groups[groupNumbers[i]], match.Groups[groupNames[i]]);

                    Assert.Equal(groupNumbers[i], regex.GroupNumberFromName(groupNames[i]));
                    Assert.Equal(groupNames[i], regex.GroupNameFromNumber(groupNumbers[i]));
                }
            }
            finally
            {
                if (cultureInfo != null || originalCulture.Equals(CultureInfo.InvariantCulture))
                {
                    CultureInfo.CurrentCulture = originalCulture;
                }
            }
        }
开发者ID:Corillian,项目名称:corefx,代码行数:40,代码来源:Regex.Groups.Tests.cs

示例4: R2LGetGroupNamesMatch

    public static void R2LGetGroupNamesMatch()
    {
        //////////// Global Variables used for all tests
        String strLoc = "Loc_000oo";
        String strValue = String.Empty;
        int iCountErrors = 0;
        int iCountTestcases = 0;
        Regex r;
        Match m;
        String s;
        String[] names;
        try
        {
            /////////////////////////  START TESTS ////////////////////////////
            ///////////////////////////////////////////////////////////////////
            s = "David Bau";
            r = new Regex("(?<first_name>\\S+)\\s(?<last_name>\\S+)");
            // [] public static String[] GetGroupNames();     "(?<first_name>\\S+)\\s(?<last_name>\\S+)"
            //-----------------------------------------------------------------
            strLoc = "Loc_498yg";
            iCountTestcases++;
            names = r.GetGroupNames();
            if (!names[0].Equals("0"))
            {
                iCountErrors++;
                Console.WriteLine("Err_234fsadg! unexpected result");
            }

            if (!names[1].Equals("first_name"))
            {
                iCountErrors++;
                Console.WriteLine("Err_234fsadg! unexpected result");
            }

            if (!names[2].Equals("last_name"))
            {
                iCountErrors++;
                Console.WriteLine("Err_234fsadg! unexpected result");
            }

            // [] public static Match Match(string input); 
            //"David Bau"
            //-----------------------------------------------------------------
            strLoc = "Loc_563sdfg";
            iCountTestcases++;
            m = r.Match(s);
            if (!m.Success)
            {
                iCountErrors++;
                Console.WriteLine("Err_87543! doesnot match");
            }

            // [] public static Match Match(string input); 
            //"David Bau"
            //-----------------------------------------------------------------
            strLoc = "Loc_298vy";
            iCountTestcases++;
            s = "12321";
            r = new Regex(@"\D+");
            if (r.IsMatch(s))
            {
                iCountErrors++;
                Console.WriteLine("Err_fsdf! doesnot match");
            }
            ///////////////////////////////////////////////////////////////////
            /////////////////////////// END TESTS /////////////////////////////
        }
        catch (Exception exc_general)
        {
            ++iCountErrors;
            Console.WriteLine("Error Err_8888yyy!  strLoc==" + strLoc + ", exc_general==" + exc_general.ToString());
        }
    }
开发者ID:nandhanurrevanth,项目名称:corefx,代码行数:73,代码来源:R2LGetGroupNamesMatchTests.cs

示例5: Run

    public bool Run()
    {
        Regex r;
        Match m;
        System.Globalization.CultureInfo originalCulture = null;

        try
        {
            if (null != _cultureInfo)
            {
                originalCulture = CultureInfo.CurrentCulture;
                CultureInfo.CurrentCulture = _cultureInfo;
            }


            try
            {
                r = new Regex(_pattern, _options);

                if (ExpectException)
                {
                    Console.WriteLine("Err_09872anba! Expected Regex to throw {0} exception and none was thrown", _expectedExceptionType);
                    return false;
                }
            }
            catch (Exception e)
            {
                if (ExpectException && e.GetType() == _expectedExceptionType)
                {
                    return true;
                }
                else if (ExpectException)
                {
                    Console.WriteLine("Err_4980asu! Expected exception of type {0} and instead the following was thrown: \n{1}", _expectedExceptionType, e);
                    return false;
                }
                else
                {
                    Console.WriteLine("Err_78394ayuua! Expected no exception to be thrown and the following was thrown: \n{0}", e);
                    return false;
                }
            }

            m = r.Match(_input);
        }
        finally
        {
            if (null != _cultureInfo)
            {
                CultureInfo.CurrentCulture = originalCulture;
            }
        }

        if (m.Success && !ExpectSuccess)
        {
            Console.WriteLine("Err_2270awanm! Did not expect the match to succeed");
            return false;
        }
        else if (!m.Success && ExpectSuccess)
        {
            Console.WriteLine("Err_68997asnzxn! Did not expect the match to fail");
            return false;
        }

        if (!ExpectSuccess)
        {
            //The match was not suppose to succeed and it failed. There is no more checking to do
            //so the test was a success
            return true;
        }

        if (m.Groups.Count != _expectedGroups.Length)
        {
            Console.WriteLine("Err_0234jah!Expected {0} groups and got {1} groups", _expectedGroups.Length, m.Groups.Count);
            return false;
        }

        if (m.Value != _expectedGroups[0])
        {
            Console.WriteLine("Err_611074ahhar Expected Value='{0}' Actual='{1}'", _expectedGroups[0], m.Value);
            return false;
        }

        int[] groupNumbers = r.GetGroupNumbers();
        string[] groupNames = r.GetGroupNames();

        for (int i = 0; i < _expectedGroups.Length; i++)
        {
            //Verify Group.Value
            if (m.Groups[groupNumbers[i]].Value != _expectedGroups[i])
            {
                Console.WriteLine("Err_07823nhhl Expected Group[{0}]='{1}' actual='{2}' Name={3}",
                    groupNumbers[i], _expectedGroups[i], m.Groups[groupNumbers[i]], r.GroupNameFromNumber(groupNumbers[i]));
                return false;
            }

            //Verify the same thing is returned from groups when using either an int or string index
            if (m.Groups[groupNumbers[i]] != m.Groups[groupNames[i]])
            {
                Console.WriteLine("Err_08712saklj Expected Groups[{0}]='{1}' Groups[{2}]='{3}'",
//.........这里部分代码省略.........
开发者ID:er0dr1guez,项目名称:corefx,代码行数:101,代码来源:Support.cs

示例6: VerifyGroups

    public static void VerifyGroups(Regex regex, string input, string[] expectedGroups, string[] expectedNames, int[] expectedNumbers)
    {
        Match match = regex.Match(input);
        Assert.True(match.Success);

        int[] numbers = regex.GetGroupNumbers();
        Assert.Equal(expectedNumbers.Length, numbers.Length);

        string[] names = regex.GetGroupNames();
        Assert.Equal(expectedNames.Length, names.Length);

        Assert.Equal(expectedGroups.Length, match.Groups.Count);
        for (int i = 0; i < expectedNumbers.Length; i++)
        {
            Assert.Equal(expectedGroups[i], match.Groups[expectedNames[i]].Value);
            Assert.Equal(expectedGroups[i], match.Groups[expectedNumbers[i]].Value);

            Assert.Equal(expectedNumbers[i], numbers[i]);
            Assert.Equal(expectedNumbers[i], regex.GroupNumberFromName(expectedNames[i]));

            Assert.Equal(expectedNames[i], names[i]);
            Assert.Equal(expectedNames[i], regex.GroupNameFromNumber(expectedNumbers[i]));
        }
    }
开发者ID:eerhardt,项目名称:corefx,代码行数:24,代码来源:GroupNamesAndNumbers.cs

示例7: GetGroupNames

 public void GetGroupNames(string pattern, string[] expectedGroupNames)
 {
     Regex regex = new Regex(pattern);
     Assert.Equal(expectedGroupNames, regex.GetGroupNames());
 }
开发者ID:ChuangYang,项目名称:corefx,代码行数:5,代码来源:Regex.GetGroupNames.Tests.cs

示例8: GroupNamesAndNumbers

        public void GroupNamesAndNumbers(string pattern, string input, string[] expectedNames, int[] expectedNumbers, string[] expectedGroups)
        {
            Regex regex = new Regex(pattern);
            Match match = regex.Match(input);
            Assert.True(match.Success);

            int[] numbers = regex.GetGroupNumbers();
            Assert.Equal(expectedNumbers.Length, numbers.Length);

            string[] names = regex.GetGroupNames();
            Assert.Equal(expectedNames.Length, names.Length);

            Assert.Equal(expectedGroups.Length, match.Groups.Count);
            for (int i = 0; i < expectedNumbers.Length; i++)
            {
                Assert.Equal(expectedGroups[i], match.Groups[expectedNames[i]].Value);
                Assert.Equal(expectedGroups[i], match.Groups[expectedNumbers[i]].Value);

                Assert.Equal(expectedNumbers[i], numbers[i]);
                Assert.Equal(expectedNumbers[i], regex.GroupNumberFromName(expectedNames[i]));

                Assert.Equal(expectedNames[i], names[i]);
                Assert.Equal(expectedNames[i], regex.GroupNameFromNumber(expectedNumbers[i]));
            }
        }
开发者ID:ChuangYang,项目名称:corefx,代码行数:25,代码来源:Regex.GetGroupNames.Tests.cs

示例9: Groups

        public void Groups(string pattern, string input, RegexOptions options, CultureInfo cultureInfo, string[] expectedGroups)
        {
            CultureInfo originalCulture = null;
            try
            {
                if (cultureInfo != null)
                {
                    originalCulture = CultureInfo.CurrentCulture;
                    CultureInfo.CurrentCulture = cultureInfo;
                }
                Regex regex = new Regex(pattern, options);
                Match match = regex.Match(input);
                Assert.True(match.Success);

                Assert.Equal(expectedGroups.Length, match.Groups.Count);
                Assert.Equal(expectedGroups[0], match.Value);

                int[] groupNumbers = regex.GetGroupNumbers();
                string[] groupNames = regex.GetGroupNames();
                for (int i = 0; i < expectedGroups.Length; i++)
                {
                    Assert.Equal(expectedGroups[i], match.Groups[groupNumbers[i]].Value);
                    Assert.Equal(match.Groups[groupNumbers[i]], match.Groups[groupNames[i]]);

                    Assert.Equal(groupNumbers[i], regex.GroupNumberFromName(groupNames[i]));
                    Assert.Equal(groupNames[i], regex.GroupNameFromNumber(groupNumbers[i]));
                }
            }
            finally
            {
                if (cultureInfo != null)
                {
                    CultureInfo.CurrentCulture = originalCulture;
                }
            }
        }
开发者ID:ChuangYang,项目名称:corefx,代码行数:36,代码来源:Regex.Groups.Tests.cs


注:本文中的Regex.GetGroupNames方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。