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


C# Regex.Execute方法代码示例

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


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

示例1: Matches

 /****************************************
 * Matches
 *
 * returns starting index of match or -1
 */
 public int Matches(string subject, string regex, RegexOption option = DefaultOption)
 {
     var r = new Regex(regex, option);
     var match = r.Execute(subject);
     if (match.Success)
         return match.Groups[0].Start;
     return -1;
 }
开发者ID:alphashack,项目名称:NET_graphdat,代码行数:13,代码来源:Parser.cs

示例2: Matching

        /****************************************
        * Matching
        *
        * returns matching substring if found, null if not or if group index is out of range (group 0 is entire match, 1 is first group etc.)
        */
        public string Matching(string subject, string regex, RegexOption option = DefaultOption, int group = 0)
        {
            var r = new Regex(regex, option);
            var match = r.Execute(subject);

            if (match.Success && match.Groups.Count > group)
                return match.Groups[group].Value;
            return null;
        }
开发者ID:alphashack,项目名称:NET_graphdat,代码行数:14,代码来源:Parser.cs

示例3: Substitute

        /****************************************
        * Substitute
        *
        * Replaces all occurrences of regex with replacement string
        */
        public string Substitute(string subject, string regex, string replace, RegexOption option = DefaultOption, int offset = 0)
        {
            // Special cases, if $N is passed then use the substring as the replacement
            // if #N is passed then use substring, pad with a space on either side
            var group = -1;
            var substituteGroup = replace.Length == 2 && (replace[0] == '$' || replace[0] == '#') &&
                                   int.TryParse(replace[1].ToString(), out group);
            var pad = !substituteGroup ? false : replace[0] == '#';

            var r = new Regex(regex, option);
            var match = r.Execute(subject);

            var replacements = new List<Replacement>();

            while (match.Success)
            {
                if (match.Groups[0].Start >= offset)
                {
                    var doReplace = false;
                    if (substituteGroup)
                    {
                        if (match.Groups.Count > group)
                        {
                            doReplace = true;
                            replace = string.Format("{0}{1}{0}", pad ? " " : "", match.Groups[group].Value);
                        }
                    }
                    else
                    {
                        doReplace = true;
                    }

                    if (doReplace)
                    {
                        replacements.Add(new Replacement
                                             {
                                                 Start = match.Groups[0].Start,
                                                 Length = match.Groups[0].Length,
                                                 Value = replace
                                             });
                    }
                }

                match = match.NextMatch;
            }

            replacements.Reverse();
            foreach (var replacement in replacements)
            {
                subject = subject.Remove(replacement.Start, replacement.Length);
                subject = subject.Insert(replacement.Start, replacement.Value);
            }

            return subject;
        }
开发者ID:alphashack,项目名称:NET_graphdat,代码行数:60,代码来源:Parser.cs


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