我有以下字符串
var tabContents = {"1":"<div class=\"ProductDetail\"><h2 class=\"h2_style\" style=\"margin: 0px 0px 15px; padding: 0px; border: 0px; vertical-align: baseline; font-weight: normal; color: rgb(0, 102, 153); letter-spacing: 0.3px; font-size: 16px; text-align: center; font-family: 'Lucida Grande', sans-serif; font-style: normal; font-variant: normal; line-height: 16px; orphans: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webv><\/div>","2":"","3":"","4":""};
现在我想得到这个部分
<div class=\"ProductDetail\"><h2 class=\"h2_style\" style=\"margin: 0px 0px 15px; padding: 0px; border: 0px; vertical-align: baseline; font-weight: normal; color: rgb(0, 102, 153); letter-spacing: 0.3px; font-size: 16px; text-align: center; font-family: 'Lucida Grande', sans-serif; font-style: normal; font-variant: normal; line-height: 16px; orphans: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webv><\/div>
因此该部分以"1":"
开头,以","2"
如何在这两个标记点之间获取字符串?
C#.net 4.5
答案 0 :(得分:3)
使用捕获组或外观。
"1":"(.*?)","2"
使用上述正则表达式并从组索引1中获取所需的字符串。
或强>
(?<="1":").*?(?=","2")
使用上面的正则表达式并从组索引0获取所需的字符串。
(?<="1":")
肯定的背后隐藏,断言匹配必须以"1":"
开头。
.*?
任意字符零次或多次出现的非贪婪匹配。
(?=","2")
确定匹配必须跟","2"
String input = @"var tabContents = {""1"":""<div class=\""ProductDetail\""><h2 class=\""h2_style\"" style=\""margin: 0px 0px 15px; padding: 0px; border: 0px; vertical-align: baseline; font-weight: normal; color: rgb(0, 102, 153); letter-spacing: 0.3px; font-size: 16px; text-align: center; font-family: 'Lucida Grande', sans-serif; font-style: normal; font-variant: normal; line-height: 16px; orphans: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webv><\/div>"",""2"":"""",""3"":"""",""4"":""""};";
Regex rgx = new Regex(@"""1"":""(.*?)"",""2""");
foreach (Match m in rgx.Matches(input))
Console.WriteLine(m.Groups[1].Value);
输出:
<div class=\"ProductDetail\"><h2 class=\"h2_style\" style=\"margin: 0px 0px 15px; padding: 0px; border: 0px; vertical-align: baseline; font-weight: normal; color: rgb(0, 102, 153); letter-spacing: 0.3px; font-size: 16px; text-align: center; font-family: 'Lucida Grande', sans-serif; font-style: normal; font-variant: normal; line-height: 16px; orphans: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webv><\/div>