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


C# Regex.GroupNameFromNumber方法代码示例

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


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

示例1: ExtractMentions

        public static IList<string> ExtractMentions(string message, IQueryable<ChatUserMention> mentions = null)
        {
            if (message == null)
                return new List<string>();

            // Find username mentions
            var matches = Regex.Matches(message, UsernameMentionPattern)
                .Cast<Match>()
                .Where(m => m.Success)
                .Select(m => m.Groups["user"].Value.Trim())
                .Where(u => !String.IsNullOrEmpty(u))
                .ToList();

            // Find custom mentions
            if (mentions == null || !mentions.Any()) return matches;

            var regex = new Regex(GetPattern(mentions), RegexOptions.IgnoreCase);

            foreach (var match in regex.Matches(message)
                                       .Cast<Match>()
                                       .Where(m => m.Success))
            {
                for (var i = 1; i < match.Groups.Count; i++)
                {
                    if (!match.Groups[i].Success) continue;

                    matches.Add(regex.GroupNameFromNumber(i));
                }
            }

            return matches;
        }
开发者ID:Widdershin,项目名称:vox,代码行数:32,代码来源:MentionExtractor.cs

示例2: match

        private void match()
        {
            treeView1.Nodes.Clear();

            try
            {
                Regex r = new Regex(textBox2.Text);

                Match m = r.Match(textBox1.Text);
                while (m.Success)
                {

                    TreeNode node = treeView1.Nodes.Add(m.Value);

                    foreach (Group g in m.Groups)
                    {
                        node.Nodes.Add(r.GroupNameFromNumber(g.Index)).Nodes.Add(g.Value);
                    }

                    m = m.NextMatch();

                }

                textBox3.Text = r.Replace(textBox1.Text, textBox4.Text);
            }
            catch (ArgumentException ex)
            {
                TreeNode node = treeView1.Nodes.Add(ex.Message);
                node.ForeColor = Color.Red;
            }
        }
开发者ID:stwalkerster,项目名称:regextester,代码行数:31,代码来源:Form1.cs

示例3: ExtractGroupings

        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        /// <param name="matchPattern"></param>
        /// <param name="wantInitialMatch"></param>
        /// <returns></returns>
        public static ArrayList ExtractGroupings(string source, string matchPattern, bool wantInitialMatch)
        {
            ArrayList keyedMatches = new ArrayList();
            int startingElement = 1;
            if (wantInitialMatch)
            {
                startingElement = 0;
            }

            Regex RE = new Regex(matchPattern, RegexOptions.Multiline);
            MatchCollection theMatches = RE.Matches(source);

            foreach (Match m in theMatches)
            {
                Hashtable groupings = new Hashtable();

                for (int counter = startingElement;
                  counter < m.Groups.Count; counter++)
                {
                    // If we had just returned the MatchCollection directly, the
                    //  GroupNameFromNumber method would not be available to use
                    groupings.Add(RE.GroupNameFromNumber(counter),
                                   m.Groups[counter]);
                }

                keyedMatches.Add(groupings);
            }

            return (keyedMatches);
        }
开发者ID:peeboo,项目名称:open-media-library,代码行数:37,代码来源:RegexUtils.cs

示例4: ExtractGroups

 public static void ExtractGroups(this string text, Dictionary<string, string> groups, string regexPattern)
 {
     var regex = new Regex(regexPattern, RegexOptions.Multiline);
     var match = regex.Match(text);
     var index = 0;
     foreach (var group in match.Groups.OfType<Group>())
         if (index++ > 0) groups.Add(regex.GroupNameFromNumber(index - 1), group.Value);
 }
开发者ID:mikeobrien,项目名称:GoNotificationInterceptor,代码行数:8,代码来源:StringExtensions.cs

示例5: GetParameters

        private static DynamicDictionary GetParameters(Regex regex, GroupCollection groups)
        {
            dynamic data = new DynamicDictionary();

            for (int i = 1; i <= groups.Count; i++)
            {
                data[regex.GroupNameFromNumber(i)] = Uri.UnescapeDataString(groups[i].Value);
            }

            return data;
        }
开发者ID:nathanpalmer,项目名称:Nancy,代码行数:11,代码来源:DefaultRoutePatternMatcher.cs

示例6: GetParameters

        private static DynamicDictionary GetParameters(Regex regex, GroupCollection groups)
        {
            dynamic data = new DynamicDictionary();

            for (var i = 1; i <= groups.Count; i++)
            {
                data[regex.GroupNameFromNumber(i)] = groups[i].Value;
            }

            return data;
        }
开发者ID:UStack,项目名称:UWeb,代码行数:11,代码来源:DefaultRoutePatternMatcher.cs

示例7: RegexMatch

 /// <summary>
 /// Initializes a new instance of the RegexMatch class.
 /// </summary>
 public RegexMatch(Match match, Regex regex)
 {
     int i = 0;
     foreach (Group g in match.Groups)
     {
         if (i != 0)
         {
             string groupName = regex.GroupNameFromNumber(i);
             _Groups.Add(new RegexGroup(g, groupName));
         }
         i++;
     }
     this._Value = match.Value;
     this._Index = match.Index;
     this._Length = match.Length;
 }
开发者ID:larrye11ison,项目名称:WpfRegexDesigner,代码行数:19,代码来源:RegexMatch.cs

示例8: GetAddressParts

        public Dictionary<string, string> GetAddressParts(string path)
        {
            if (path == null) return null;
            if (_url == null) return null;
            var pattern = new Regex(_url);
            var match = pattern.Match(path);

            var addressParts = new Dictionary<string, string>();
            var groupCollection = match.Groups;
            for (var i = 1; i <= groupCollection.Count; i++)
            {
                var key = pattern.GroupNameFromNumber(i);
                var value = groupCollection[i].Value;
                addressParts.Add(key, value);
            }
            return addressParts;
        }
开发者ID:bnathyuw,项目名称:nonae,代码行数:17,代码来源:PathAnalyser.cs

示例9: Matches

        // Public Methods
        public TreeNode[] Matches(string content, Regex regex)
        {
            MatchCollection matches = regex.Matches(content);
            List<TreeNode> nodesOfMatches = new List<TreeNode>(matches.Count);

            for (int i = 0; i < matches.Count; i++)
            {
                TreeNode node = new TreeNode(NodeString(i, matches[i].Value));
                node.Tag = new HighlightInfo()
                {
                    Index = matches[i].Index,
                    Length = matches[i].Length
                };

                // start from 1, because 0 is match itself
                for (int j = 1; j < matches[i].Groups.Count; j++)
                {
                    TreeNode childNode = new TreeNode(NodeString(j, regex.GroupNameFromNumber(j), matches[i].Groups[j].Value));
                    childNode.Tag = new HighlightInfo()
                    {
                        Index = matches[i].Groups[j].Index,
                        Length = matches[i].Groups[j].Length
                    };

                    for (int k = 0; k < matches[i].Groups[j].Captures.Count; k++)
                    {
                        TreeNode childchildNode = new TreeNode(NodeString(k, matches[i].Groups[j].Captures[k].Value));
                        childchildNode.Tag = new HighlightInfo()
                        {
                            Index = matches[i].Groups[j].Captures[k].Index,
                            Length = matches[i].Groups[j].Captures[k].Length
                        };

                        childNode.Nodes.Add(childchildNode);
                    }

                    node.Nodes.Add(childNode);
                }

                nodesOfMatches.Add(node);
            }

            return nodesOfMatches.ToArray();
        }
开发者ID:angieduan,项目名称:regexsharp,代码行数:45,代码来源:RegexEngine.cs

示例10: GetCapturedMatches

        private static IList<Scope> GetCapturedMatches(Match regexMatch, CompiledMacro macro,
                                                       IList<Scope> capturedMatches, Regex regex)
        {
            for (int i = 0; i < regexMatch.Groups.Count; i++)
            {
                if (regex.GroupNameFromNumber(i) != i.ToString())
                    continue;

                Group regexGroup = regexMatch.Groups[i];
                string capture = macro.Captures[i];

                if (regexGroup.Captures.Count == 0 || String.IsNullOrEmpty(capture))
                    continue;

                foreach (Capture regexCapture in regexGroup.Captures)
                    AppendCapturedMatchesForRegexCapture(regexCapture, capture, capturedMatches);
            }

            return capturedMatches;
        }
开发者ID:atachimiko,项目名称:AtachiWiki,代码行数:20,代码来源:Parser.cs

示例11: Request

        /// <summary>
        /// Initializes a new instance of the <see cref="Request"/> class.
        /// </summary>
        /// <param name="fullrequest">The full request string.</param>
        public Request(string fullrequest, Regex rx)
        {
            HasSessionId = false;
            GET = new Hashtable();
            POST = new Hashtable();
            CLIENT = new Hashtable();
            COOKIE = new Hashtable();
            SESSION = new Hashtable();
            string line = fullrequest.Substring(0, fullrequest.IndexOf("\r\n"));
            int firstspace = line.IndexOf(' ') + 1;
            RequestedPath = line.Substring(firstspace, line.LastIndexOf(' ') - firstspace);
            Match match = rx.Match(RequestedPath);
            for (int i = 1; i < match.Groups.Count; i++)
            {
                GET.Add(rx.GroupNameFromNumber(i), HttpUtility.UrlDecode(match.Groups[i].Value));
            }
            Regex sessidrx = new Regex(@"ISESSION=([A-Z0-9]{16})", RegexOptions.Compiled);
            if (sessidrx.IsMatch(fullrequest))
            {
                HasSessionId = true;
                Match m = sessidrx.Match(fullrequest);
                GroupCollection g = m.Groups;
                SessionId = g[1].Value;
            }
            if (line.Split(new char[] { ' ' })[0] == "POST")
            {
                Regex rx2 = new Regex(@"\&*([a-zA-Z0-9_]+)=([^\&]+)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                MatchCollection Matches = rx2.Matches(fullrequest.Substring(fullrequest.IndexOf("\r\n\r\n")+4));
                foreach (Match m in Matches)
                {
                    GroupCollection groups = m.Groups;
                    string val = groups[2].Value;
                    if (val.StartsWith("&"))
                    {
                        val = val.Substring(1);
                    }

                    POST.Add(groups[1].Value, HttpUtility.UrlDecode(val));
                }
            }
        }
开发者ID:theanti9,项目名称:irek,代码行数:45,代码来源:Request.cs

示例12: Evaluate

        public string Evaluate(string inputText, Regex regex)
        {
            var sb = new StringBuilder();
            var groups = regex.GetGroupNames();
            if (groups.Length > 1)
            {
                sb.AppendLine(string.Format("Groups: {0}", string.Join(", ", groups.Skip(1))));
                sb.AppendLine();
            }

            foreach (Match match in regex.Matches(inputText))
            {
                sb.AppendLine(string.Format("Match: {0}", match.Value));
                for (var i = 1; i < match.Groups.Count; i++)
                {
                    var g = match.Groups[i];
                    if (g.Success)
                    {
                        sb.AppendLine(string.Format("    Group {0}: {1}", regex.GroupNameFromNumber(i), g.Value));
                    }
                }
            }
            return sb.ToString();
        }
开发者ID:heinersuter,项目名称:simple-ui-tests,代码行数:24,代码来源:RegexEvaluator.cs

示例13: button1_Click

        private void button1_Click(object sender, EventArgs e)
        {
            // Define a regular expression
            try
            {
                Regex rx = new Regex(txtRegex.Text, RegexOptions.Compiled | RegexOptions.IgnoreCase);

                // Find matches.
                MatchCollection matches = rx.Matches(txtSource.Text);

                txtMatch.Text = "";

                foreach (Match match in matches)
                {
                    // Report the number of matches found.
                    //Console.WriteLine("{0} matches found in: {1} value: {2}\n", matches.Count, txtSource.Text, match.Groups[1].ToString());

                    //txtMatch.Text = match.Groups[1].ToString();

                    for (int i = 1; i < match.Groups.Count; i++)
                    {
                        var group = match.Groups[i];
                        if (group.Success)
                            txtMatch.Text = txtMatch.Text + "Group: " + rx.GroupNameFromNumber(i) + "  Value: " + match.Groups[i].ToString() + "\r\n";

                    }

                }
            }
            catch(Exception x)
            {
                // Catch the exception and display the error message.
                MessageBox.Show(x.Message, "Oops.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

            }
        }
开发者ID:jeffld,项目名称:TestRegex,代码行数:36,代码来源:Form1.cs

示例14: Instance_Deny_Unrestricted

		public void Instance_Deny_Unrestricted ()
		{
			Regex r = new Regex (String.Empty);
			Assert.AreEqual (RegexOptions.None, r.Options, "Options");
			Assert.IsFalse (r.RightToLeft, "RightToLeft");

			Assert.AreEqual (1, r.GetGroupNames ().Length, "GetGroupNames");
			Assert.AreEqual (1, r.GetGroupNumbers ().Length, "GetGroupNumbers");
			Assert.AreEqual ("0", r.GroupNameFromNumber (0), "GroupNameFromNumber");
			Assert.AreEqual (0, r.GroupNumberFromName ("0"), "GroupNumberFromName");

			Assert.IsTrue (r.IsMatch (String.Empty), "IsMatch");
			Assert.IsTrue (r.IsMatch (String.Empty, 0), "IsMatch2");

			Assert.IsNotNull (r.Match (String.Empty), "Match");
			Assert.IsNotNull (r.Match (String.Empty, 0), "Match2");
			Assert.IsNotNull (r.Match (String.Empty, 0, 0), "Match3");

			Assert.AreEqual (1, r.Matches (String.Empty).Count, "Matches");
			Assert.AreEqual (1, r.Matches (String.Empty, 0).Count, "Matches2");

			Assert.AreEqual (String.Empty, r.Replace (String.Empty, new MatchEvaluator (Evaluator)), "Replace");
			Assert.AreEqual (String.Empty, r.Replace (String.Empty, new MatchEvaluator (Evaluator), 0), "Replace2");
			Assert.AreEqual (String.Empty, r.Replace (String.Empty, new MatchEvaluator (Evaluator), 0, 0), "Replace3");
			Assert.AreEqual (String.Empty, r.Replace (String.Empty, String.Empty), "Replace4");
			Assert.AreEqual (String.Empty, r.Replace (String.Empty, String.Empty, 0), "Replace5");
			Assert.AreEqual (String.Empty, r.Replace (String.Empty, String.Empty, 0, 0), "Replace6");

			Assert.AreEqual (2, r.Split (String.Empty).Length, "Split");
			Assert.AreEqual (2, r.Split (String.Empty, 0).Length, "Split2");
			Assert.AreEqual (2, r.Split (String.Empty, 0, 0).Length, "Split3");

			Assert.AreEqual (String.Empty, r.ToString (), "ToString");
		}
开发者ID:nlhepler,项目名称:mono,代码行数:34,代码来源:RegexCas.cs

示例15: AddGroupNameToResult

        private static void AddGroupNameToResult(Regex regex,PhpArray matches, int i, Action<PhpArray, string> action)
        {
            // named group?
            var groupName = regex.GroupNameFromNumber(i);
            //remove sign from the beginning of the groupName
            groupName = groupName[0] == PerlRegExpConverter.GroupPrefix ? groupName.Remove(0, 1) : groupName;

            if (!String.IsNullOrEmpty(groupName) && groupName != i.ToString())
            {
                action(matches, groupName);
            }
        }
开发者ID:jdluzen,项目名称:Phalanger,代码行数:12,代码来源:RegExpPerl.cs


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