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("
{0} 共有 {1} 个分组:{2}
" , match.Value, groups.Count, pattern)); //提取匹配项内的分组信息 for (int i = 0; i < groups.Count; i++) { Console.WriteLine( string.Format("分组 {0} 为 {1},位置为 {2},长度为 {3}
" , i , groups[i].Value , groups[i].Index , groups[i].Length)); } } //提取匹配项 foreach (Match match in matches) { CaptureCollection captures = match.Captures; Console.WriteLine(string.Format("
{0} 共有 {1} 个匹配:{2}
" , match.Value, captures.Count, pattern)); //提取匹配项内的分组信息 for (int i = 0; i < captures.Count; i++) { Console.WriteLine( string.Format("匹配 {0} 为 {1},位置为 {2},长度为 {3}
" , i , captures[i].Value , captures[i].Index , captures[i].Length)); } } Console.ReadKey(); } }

4、输出结果

C# Regex Match,Group,Capture示例_第1张图片

 

你可能感兴趣的:(C# Regex Match,Group,Capture示例)