c# - 正则表达式,在两个词之间找一个词

标签 c# .net regex

我有这个字符串

<p/><ul><li>test1<p/></li><li>test2<p/></li></ul><p/>

我尝试做的是提取“li”标签内的所有“p”标签,但不提取它之外的“p”标签。

目前我只能 通过

提取所有“li”标签
\<li\>(.*?)\</li\>

我不知道如何提取其中的“p”标签。

非常感谢任何指针!!

最佳答案

使用 HTML 解析器而不是正则表达式要可靠得多。使用 HTML Agility Pack :

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml("<p/><ul><li>test1<p/></li><li>test2<p/></li></ul><p/>");
IEnumerable<HtmlNode> result = doc.DocumentNode
                                  .Descendants("li")
                                  .SelectMany(x => x.Descendants("p"));

关于c# - 正则表达式,在两个词之间找一个词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2390359/

相关文章:

C# GemBox-SpreadSheet 从右到左

c# - Apache Axis 客户端、ASMX 服务、阵列不兼容问题

c# - 循环 ILookup,访问值

regex - 如何匹配以 "I"开头但不是 "Integer"的单词?

C# - 内部成员的命名约定

c# - 在 WebRequest 中发送压缩数据?

c# - 使用 C# 获取文件夹中包含大小的文件元数据的快速方法是什么?

c++ - 我可以用它来代替 ATL 的使用以获得相同或更好的性能?

JavaScript 正则表达式 : Is there a way to match slash after slash char in URL without negative lookbehind?

regex - 正则表达式替换为sed