zoukankan      html  css  js  c++  java
  • 正则匹配

    1,匹配标签里面的内容

         StringBuilder sbStr = new StringBuilder();
                sbStr.Append("<Head>");
                sbStr.Append(" ");
                sbStr.Append("<Title>Ben Forta's HomePage</Title>");
                sbStr.Append(" ");
                sbStr.Append("</Head>");

                string expression = @"(?<=<title>).*?(?=</title>)";
                Regex reg = new Regex(expression, RegexOptions.IgnoreCase);
                MatchCollection mc = reg.Matches(sbStr.ToString());
                foreach (Match match in mc)
                {
                    Console.WriteLine(match.Value);
                }
                Console.ReadKey();

    2.匹配json

     string str = "{"row1":[{"YYYWBH":"030301030000001","SLDW":"440309000000","WSYYSJ":"2015-06-11 10:00-11:00"},{"YYYWBH":"030301030000001","SLDW":"440309000000","WSYYSJ":"2015-06-11 10:00-11:00"}],"returnCode":"1","returnInfo":""}";

      Regex regex = new Regex(@"""YYYWBH"":s*""(?'YYYWBH'[^""]*)"",s*""SLDW"":s*(?'SLDW'[^,]*),s*""WSYYSJ"":s*(?'WSYYSJ'[^}]*)", RegexOptions.IgnoreCase);
                //申明和实例化一个正则表达式对象,这里要加上参数RegexOptions.IgnoreCase,用于忽略文本大小写
                if (regex.IsMatch(str))
                {
                    MatchCollection matches = regex.Matches(str);
                    //上处可以匹配文本里有多组相似的结果全部结果,如果只有一组的话,用Match match = regex.Match(json);
                    StringBuilder stringBuilder = new StringBuilder();
                    foreach (Match match in matches)
                    {
                        string name = match.Groups["YYYWBH"].Value;//此处Name是匹配表达式(?'Name'[^"]*)里的Name,显式分组
                        string age = match.Groups["SLDW"].Value;
                        string married = match.Groups["WSYYSJ"].Value;
                        stringBuilder.AppendFormat("YYYWBH:{0},SLDW:{1},WSYYSJ:{2} ", name, age, married);
                    }
                    Console.WriteLine(stringBuilder.ToString());
                }

  • 相关阅读:
    thinkphp3.2源码(错误和异常处理)
    linux升级openssl和php_openssl模块
    详解PhpSpreadsheet设置单元格
    MySQL字符集 utf8 和 utf8mb4 区别及排序规则 general_ci 和 unicode_ci 和 bin 的区别
    Cocos Creator cc.Button (脚本事件内容)
    cocos creator 重写源码按钮Button点击音频封装
    Cocos Creator JS web平台复制粘贴代码(亲测可用)
    JS 获取最近(前)7天(一周内)和最近(前)3天日期
    Cocos Creator 构建发布... APP ABI(选项)
    Cocos Creator JS 时间戳日期转换
  • 原文地址:https://www.cnblogs.com/zhangweixin/p/4587822.html
Copyright © 2011-2022 走看看