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

时间:2021-05-20

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

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章