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、输出结果
本博客是个人工作中记录,更深层次的问题可以提供有偿技术支持。
另外建了几个QQ技术群:
2、全栈技术群:616945527
2、硬件嵌入式开发: 75764412
3、Go语言交流群:9924600
闲置域名WWW.EXAI.CN (超级人工智能)出售。
另外建了几个QQ技术群:
2、全栈技术群:616945527
2、硬件嵌入式开发: 75764412
3、Go语言交流群:9924600
闲置域名WWW.EXAI.CN (超级人工智能)出售。