c#字符串應用正則表達式示例。本站提示廣大學習愛好者:(c#字符串應用正則表達式示例)文章只能為提供參考,不一定能成為您想要的結果。以下是c#字符串應用正則表達式示例正文
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