项目开发中和第三方系统API对接经常会涉及到json数据的解析问题、使用正则可以快速匹配到关键字值
///
/// 正则表达式
///
private class RegexMatch {
///
/// 在输入字符串中搜索pattern参数中提供的一个或多个文本
///
///
///
///
///
public static string Match(string s, string p, int i = 0) {
if (string.IsNullOrEmpty(s) || string.IsNullOrEmpty(p)) return string.Empty;
var match = Regex.Match(s, p);
if (i < match.Groups.Count) return match.Groups[i].Value;
i = 0;
return match.Groups[i].Value;
}
///
/// 字符串
///
///
///
///
public static string Getstring(string str, string name) { return Match(str, "\"" + name + @""":""([^""]+)", 1); }
///
/// 数值
///
///
///
///
public static string Getmoney(string str, string name) { return Match(str, "\"" + name + @""":""?([\-\d\.]+)", 1); }
}
var str = "
str = GetTitleContent(str, "tem:PresentApplicationJson");
///
/// 获取字符中指定标签的值
///
/// 字符串
/// 标签
///
public static string GetTitleContent(string str, string title) {
string tmpStr = string.Format("<{0}[^>]*?>(?
Match TitleMatch = Regex.Match(str, tmpStr, RegexOptions.IgnoreCase);
string result = TitleMatch.Groups["Text"].Value;
return result;
}
///
/// 获取字符中指定标签的值
///
/// 字符串
/// 标签
/// 属性名
///
public static string GetTitleContent(string str, string title,string attrib)
{
string tmpStr = string.Format("<{0}[^>]*?{1}=(['\"\"]?)(?
Match TitleMatch = Regex.Match(str, tmpStr, RegexOptions.IgnoreCase);
string result = TitleMatch.Groups["url"].Value;
return result;
}
参考《https://www.cnblogs.com/vipsoft/p/3236960.html》