C# 如何使用正则表达式提取字符串的这一部分

C# 如何使用正则表达式提取字符串的这一部分,c#,regex,C#,Regex,我有以下字符串 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; fon

我有以下字符串

  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":""};

var-tabContents={“1”:“使用捕获组或查找区

"1":"(.*?)","2"
使用上面的正则表达式并从组索引1中获取所需的字符串

(?<="1":").*?(?=","2")
输出:

<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>

这是正确的,但是,我想使用ReGEX,因为某些原因,所以我需要用ReXEX来把ReGEX作为字符串来做,把我的结果带出来。例如+是一个特殊的字符。如果它是以+开头的,我会怎样用正则表达式来处理它呢?字符串?使用
\+
匹配一个文本
+
ty作为答案。因此,对于我需要在其前面添加\的任何特殊字符?是的,您需要在每个正则表达式元字符之前添加\以获得其文本形式。好的,我尝试了第一个。它工作正常,但仍然包括开头和结尾。因此,它们之间是否有性能差异n 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>