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、输出结果

 

posted @ 2020-01-17 23:00  zhaogaojian  阅读(899)  评论(0编辑  收藏  举报