正则表达式在标签C#之间查找文本

时间:2022-09-13 08:31:21

I want to strip the html tags and only return the text between the tags. Here is what I'm currently using.

我想剥离html标签,只返回标签之间的文本。这是我目前正在使用的。

string regularExpressionPattern1 = @"<td(.*?)<\/td>";
Regex regex = new Regex(regularExpressionPattern1, RegexOptions.Singleline);
MatchCollection collection = regex.Matches(value.ToString());

I currently get <td>13</td>, and I just want 13.

我目前得到 13 ,我只想要13。

Thanks,

4 个解决方案

#1


3  

You need to get value of group not of the match. Try this

您需要获得不属于该组的组值。试试这个

Match m = collection[0];
var stripped = m.Groups[1].Value;

#2


3  

So, using the HTML AgilityPack, this would be really easy...

所以,使用HTML AgilityPack,这真的很容易......

 HtmlDocument  doc = doc.LoadHtml(value);
 var nodes = doc.DocumentNode.SelectNodes("//td//text()");

Puts the TextNodes in the nodes variable.

将TextNodes放在nodes变量中。

#3


3  

You can use look-behind ?<= and look-ahead ?= like this:

你可以使用look-behind?<= and look-ahead?=喜欢这个:

(?<=<td>)(.*?)(?=<\/td>)

That should give you just the text between the tags. More info on Regex and look-ahead/look-behind can be found Here.

这应该只给你标签之间的文字。有关正则表达式和前瞻/后视的更多信息,请点击此处。

Also, a good Regex tester can be found Here. I use it to test all my Regex strings when I'm writing them.

此外,可以在这里找到一个好的Regex测试仪。我用它来测试我写的所有正则表达式字符串。

#4


-1  

use match.Groups[1].Value

#1


3  

You need to get value of group not of the match. Try this

您需要获得不属于该组的组值。试试这个

Match m = collection[0];
var stripped = m.Groups[1].Value;

#2


3  

So, using the HTML AgilityPack, this would be really easy...

所以,使用HTML AgilityPack,这真的很容易......

 HtmlDocument  doc = doc.LoadHtml(value);
 var nodes = doc.DocumentNode.SelectNodes("//td//text()");

Puts the TextNodes in the nodes variable.

将TextNodes放在nodes变量中。

#3


3  

You can use look-behind ?<= and look-ahead ?= like this:

你可以使用look-behind?<= and look-ahead?=喜欢这个:

(?<=<td>)(.*?)(?=<\/td>)

That should give you just the text between the tags. More info on Regex and look-ahead/look-behind can be found Here.

这应该只给你标签之间的文字。有关正则表达式和前瞻/后视的更多信息,请点击此处。

Also, a good Regex tester can be found Here. I use it to test all my Regex strings when I'm writing them.

此外,可以在这里找到一个好的Regex测试仪。我用它来测试我写的所有正则表达式字符串。

#4


-1  

use match.Groups[1].Value