zoukankan      html  css  js  c++  java
  • C# Regex Match,Group,Capture示例

    字符串是企业微信返回的考勤数据,找出所有打卡时间的功能

    1、原始数据

    {"errcode":0,"errmsg":"ok","checkindata":[{"userid":"PanPengYan","groupname":"固定1","checkin_type":"上班打卡","exception_type":"未打卡","checkin_time":1579395600,"location_title":"","location_detail":"","wifiname":"","notes":"","wifimac":"","mediaids":[],"lat":0,"lng":0},{"userid":"ZhaoGaoJian","groupname":"固定1","checkin_type":"上班打卡","exception_type":"未打卡","checkin_time":1579395600,"location_title":"","location_detail":"","wifiname":"","notes":"","wifimac":"","mediaids":[],"lat":0,"lng":0},{"userid":"PanPengYan","groupname":"固定1","checkin_type":"上班打卡","exception_type":"未打卡","checkin_time":1579424460,"location_title":"","location_detail":"","wifiname":"","notes":"","wifimac":"","mediaids":[],"lat":0,"lng":0},{"userid":"ZhaoGaoJian","groupname":"固定1","checkin_type":"上班打卡","exception_type":"未打卡","checkin_time":1579424460,"location_title":"","location_detail":"","wifiname":"","notes":"","wifimac":"","mediaids":[],"lat":0,"lng":0}]}

    2、正则表达式

    checkin_time":(d+)\,

    3、最终代码

    class Program
        {
            static void Main(string[] args)
            {
                
                string text =
                    "{"errcode":0,"errmsg":"ok","checkindata":[{"userid":"PanPengYan","groupname":"固定1","checkin_type":"上班打卡","exception_type":"未打卡","checkin_time":1579395600,"location_title":"","location_detail":"","wifiname":"","notes":"","wifimac":"","mediaids":[],"lat":0,"lng":0},{"userid":"ZhaoGaoJian","groupname":"固定1","checkin_type":"上班打卡","exception_type":"未打卡","checkin_time":1579395600,"location_title":"","location_detail":"","wifiname":"","notes":"","wifimac":"","mediaids":[],"lat":0,"lng":0},{"userid":"PanPengYan","groupname":"固定1","checkin_type":"上班打卡","exception_type":"未打卡","checkin_time":1579424460,"location_title":"","location_detail":"","wifiname":"","notes":"","wifimac":"","mediaids":[],"lat":0,"lng":0},{"userid":"ZhaoGaoJian","groupname":"固定1","checkin_type":"上班打卡","exception_type":"未打卡","checkin_time":1579424460,"location_title":"","location_detail":"","wifiname":"","notes":"","wifimac":"","mediaids":[],"lat":0,"lng":0}]}";
                string pattern = @"checkin_time"":(d+)\,";
                //MatchCollection mc = Regex.Matches(text, pattern);
                Regex rex = new Regex(pattern, RegexOptions.IgnoreCase);
                MatchCollection matches = rex.Matches(text);
    
                //提取匹配项
                foreach (Match match in matches)
                {
                    GroupCollection groups = match.Groups;
                    Console.WriteLine(string.Format("<br/>{0} 共有 {1} 个分组:{2}<br/>"
                        , match.Value, groups.Count, pattern));
    
                    //提取匹配项内的分组信息
                    for (int i = 0; i < groups.Count; i++)
                    {
                        Console.WriteLine(
                            string.Format("分组 {0} 为 {1},位置为 {2},长度为 {3}<br/>"
                                , i
                                , groups[i].Value
                                , groups[i].Index
                                , groups[i].Length));
                    }
                }
                //提取匹配项
                foreach (Match match in matches)
                {
                    CaptureCollection captures = match.Captures;
                    Console.WriteLine(string.Format("<br/>{0} 共有 {1} 个匹配:{2}<br/>"
                        , match.Value, captures.Count, pattern));
    
                    //提取匹配项内的分组信息
                    for (int i = 0; i < captures.Count; i++)
                    {
                        Console.WriteLine(
                            string.Format("匹配 {0} 为 {1},位置为 {2},长度为 {3}<br/>"
                                , i
                                , captures[i].Value
                                , captures[i].Index
                                , captures[i].Length));
                    }
                }
                Console.ReadKey();
            }
        }

    4、输出结果

  • 相关阅读:
    Construct Binary Tree from Preorder and Inorder Traversal
    Construct Binary Tree from Inorder and Postorder Traversal
    Maximum Depth of Binary Tree
    Sharepoint 2013 创建TimeJob 自动发送邮件
    IE8 不能够在Sharepoint平台上在线打开Office文档解决方案
    TFS安装与管理
    局域网通过IP查看对方计算机名,通过计算机名查看对方IP以及查看在线所有电脑IP
    JS 隐藏Sharepoint中List Item View页面的某一个字段
    SharePoint Calculated Column Formulas & Functions
    JS 两个一组数组转二维数组
  • 原文地址:https://www.cnblogs.com/zhaogaojian/p/12218708.html
Copyright © 2011-2022 走看看