zoukankan      html  css  js  c++  java
  • C# 正则表达式贪婪模式案例

    案例一、

    如 "acbacb"  正则  "a.*?b" 只会取到第一个"acb" 原本可以全部取到但加了限定符后,只会匹配尽可能少的字符 ,而"acbacb"最少字符的结果就是"acb" 。

    案例二、

    /// <summary>
            /// 去掉<script>标签
            /// </summary>
            static void Test2()
            {
                //要匹配的字符串  
                string text = @"hello
                                <script>
                                alert('1');
                                </script>
                                regex
                                <script>
                                alert('2');
                                </script>
                                world";
                //正则表达式  
                string pattern = @"<script>[sS]*?</script>";
                //string result = Regex.Replace(text, pattern, "").Replace("
    ","").Replace(" ","");
                string result = Regex.Replace(text, pattern, "").Replace(" ", "");
                Console.WriteLine(result);
            }
    
            /// <summary>
            /// 获取<script>标签内容
            /// </summary>
            static void Test3()
            {
                //要匹配的字符串  
                string text = @"hello
                                <script>
                                alert('1');
                                </script>
                                regex
                                <script>
                                alert('2');
                                </script>
                                world";
                //正则表达式  
                string pattern = @"<script>[sS]*?</script>";
                Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
                //string result = Regex.Replace(text, pattern, "").Replace("
    ","").Replace(" ","");
                MatchCollection matchCollection = r.Matches(text);
                foreach (Match m in matchCollection)
                {
                    //显示匹配开始处的索引值和匹配到的值  
                    System.Console.WriteLine("Match=[" + m + "]");
                    CaptureCollection cc = m.Captures;
                    foreach (Capture c in cc)
                    {
                        Console.WriteLine("/tCapture=[" + c + "]");
                    }
                    for (int i = 0; i < m.Groups.Count; i++)
                    {
                        Group group = m.Groups[i];
                        System.Console.WriteLine("/t/tGroups[{0}]=[{1}]", i, group);
                        for (int j = 0; j < group.Captures.Count; j++)
                        {
                            Capture capture = group.Captures[j];
                            Console.WriteLine("/t/t/tCaptures[{0}]=[{1}]", j, capture);
                        }
                    }
                }
            }
  • 相关阅读:
    HTML5学习笔记简明版(1):HTML5介绍与语法
    用margin还是用padding(1)——W3School CSS外边距合并
    Minimum Depth of Binary Tree
    118. Pascal's Triangle
    Convert Sorted Array to Binary Search Tree
    112. Path Sum
    Balanced Binary Tree
    centos 7下nginx搭建流媒体服务器【动态添加模块】
    Java内存泄漏
    Quartz的job中注入的services接口为空的解决办法
  • 原文地址:https://www.cnblogs.com/zoro-zero/p/7372806.html
Copyright © 2011-2022 走看看