C#中,使用正则表达式匹配获取所需数据
2024-10-19 02:25:29
.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);
这时候tempStr得到的是”-1--{"UserId" : "1000","userName" : "ZhangSan"}“
最新文章
- 【HDU 2874】Connections between cities(LCA)
- delphi XE Berlin ReadProcessMemory WriteProcessMemory
- 【228】◀▶ Excel 函数说明
- Js数组里删除指定的元素(不是指定的位置)
- 带你揭开ATM的神秘面纱
- IOS 中的MVC设计模式
- Oracle本地,远程,分布式登录
- 使用rowid和rownum获取记录时要注意的问题
- ASP.NET身份验证的几种方式
- 玩转python之字符串逐个字符或逐词反转
- Python网络数据采集7-单元测试与Selenium自动化测试
- HDU - 1827 Summer Holiday (强连通)
- 【Spark篇】---Spark中transformations算子二
- js 中常用的循环
- ab与nc命令,tcpdump命令
- centos 7安装java开发环境
- http协议tcp协议ip协议三次握手四次挥手,为什么三次握手,为什么四次挥手,sockete套接字理解
- Sonya and Exhibition 1004B
- java字符串的遍历以及字符串中各类字符的统计
- Struts hibernate Spring 框架原理
热门文章
- 二叉苹果树|codevs5565|luoguP2015|树形DP|Elena
- 使用FFmpeg常见问题
- Installing Precise (12.04.2) using netboot onto a Marvell ArmadaXP Development Board
- wpf(windos窗体)
- Xcode编译警告Assigning to &#39;id<;XXXDelegat>; ——Nullable&#39; from incompatible type &#39;XXXView *const_strong&#39;
- [others] 一个酷酷的站
- day3_元组
- LeetCode 104 Maximum Depth of Binary Tree 解题报告
- XSL常用用法语句
- DER、CRT、CER、PEM格式的证书及转换