Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# html标记中的正则表达式匹配_C#_Regex - Fatal编程技术网

C# html标记中的正则表达式匹配

C# html标记中的正则表达式匹配,c#,regex,C#,Regex,我需要对以下两种类型的输出进行正则表达式匹配和替换: <span class=Price>$9.99</span> <span class=RegularPrice>$9.99</span><span class=SalePrice>$4.99</span> 9.99美元 $9.99$4.99 我想做的是在第一个例子中匹配9.99美元;第二天是4.99美元。(如果类是Price或SalePrice,则基本上与标记的内容匹

我需要对以下两种类型的输出进行正则表达式匹配和替换:

<span class=Price>$9.99</span>
<span class=RegularPrice>$9.99</span><span class=SalePrice>$4.99</span>
9.99美元
$9.99$4.99
我想做的是在第一个例子中匹配9.99美元;第二天是4.99美元。(如果类是Price或SalePrice,则基本上与标记的内容匹配

我试过几个例子;但它们要么匹配整个跨度;要么从SalePrice开始,然后继续

我现在得到的是:

var regex = new Regex(@"<span class=Price|SalePrice>(.*?)<\/span>");
var regex=new regex(@“(.*)”;

但是在某个地方有点偏离。我认为这种交替不太正确;有人能给我指出正确的方向吗?

这会一直给你提供跨度

"<span class=(Price|SalePrice)>(.*?)<\/span>"
“(.*)”

如果您不想与价格/售价匹配,则应使用:

<span class=(?:Price|SalePrice)?>(.*?)<\/span>
(*)

幸运的是,.NET支持可变长度lookbehind:

(?<=<span\s+class=\1?(?:Price|SalePrice)(['"])?>)([^<]*)(?=<\/span>)

(?在正则表达式上下文中使用管道(不在组
(…)
)将从左到右分隔所有内容。这意味着您实际需要
(?:Price | SalePrice)