当前位置: 移动技术网 > IT编程>开发语言>c# > c#字符串使用正则表达式示例

c#字符串使用正则表达式示例

2019年07月18日  | 移动技术网IT编程  | 我要评论
1.截取字符串中指定内容 复制代码 代码如下:{"weatherinfo":{"city":"北京","cityid":"101010100","temp":"-4","

1.截取字符串中指定内容

复制代码 代码如下:

{"weatherinfo":{"city":"北京","cityid":"101010100","temp":"-4","wd":"西北风","ws":"2级","sd":"29%","wse":"2","time":"09:40","isradar":"1","radar":"jc_radar_az9010_jb"}}

复制代码 代码如下:

string pattern = "{\"weatherinfo\":(.*)}";
var result = regex.match(weatherqueryresult, pattern, regexoptions.ignorecase).groups;

复制代码 代码如下:

返回结果为{"city":"北京","cityid":"101010100","temp":"-4","wd":"西北风","ws":"2级","sd":"29%","wse":"2","time":"09:40","isradar":"1","radar":"jc_radar_az9010_jb"}

2.截取字符串中的数字

复制代码 代码如下:

string s = "b123-c12";
matchcollection vmatchs = regex.matches(s, @"(\d+)");
vmatchs[0].value

返回结果 123,12

3.截取字符串中的字母

复制代码 代码如下:

string str = "呵呵呵呵ab-cfe-sdfeww";
matchcollection m = regex.matches(str, @"[a-z]+");//小写字母为a-z 大小写混合为a-za-z

返回结果为b/f/e

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网