欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-20 16:11:40
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

上一篇:

下一篇: