星空网 > 软件开发 > ASP.net

C#中,使用正式表达式匹配获取所需数据

.NET中,使用正式表达式匹配获取所需数据

 

需求:获取一串字符串中,正则匹配出需要的数据。

例如以下字符串:

string temp ="ErrorCode:-1,Message:{"UserId" : "1000","userName" : "ZhangSan"}";

我需要获得“-1”和“{"UserId" : "1000","userName" : "ZhangSan"}”;

 

接下来,就使用正则去匹配:

using System.Text.RegularExpressions;string temp = "ErrorCode:-1,Message:{\"UserId\" : \"1000\",\"userName\" : \"ZhangSan\"}";Regex reg = new Regex("ErrorCode:(?<key1>.*?),Message:{(?<key2>.*?)}");Match match = reg.Match(temp);string tempStr = match.Groups["key1"].Value + "--" + match.Groups["key2"].Value;MessageBox.Show(tempStr);

C#中,使用正式表达式匹配获取所需数据

 

这时候tempStr得到的是”-1--{"UserId" : "1000","userName" : "ZhangSan"}“




原标题:C#中,使用正式表达式匹配获取所需数据

关键词:C#

C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

美国市场:https://www.goluckyvip.com/tag/3710.html
狂欢节:https://www.goluckyvip.com/tag/37100.html
邝玉华:https://www.goluckyvip.com/tag/37101.html
葵花岛:https://www.goluckyvip.com/tag/37102.html
坤鑫国际:https://www.goluckyvip.com/tag/37103.html
坤鑫国际货运代理:https://www.goluckyvip.com/tag/37104.html
深圳到西安自驾路线攻略 深圳到西安自驾最佳路线:https://www.vstour.cn/a/411228.html
松花蛋是哪里的特产松花蛋的产地:https://www.vstour.cn/a/411229.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流