简体   繁体   English

C# 正则表达式多重匹配

[英]C# Regex multiple matching

I have this regex to extract paragraphs that are outside of a table我有这个正则表达式来提取表格之外的段落

((?<=<\/w:tbl>)<w:p [^>]*>.*?<\/w:p>(?=<w:tbl>)|(?<=<\/w:tbl>)<w:p [^>]*>.*?<\/w:p>(?=<w:sectPr.*>))

The problem is that it reads all paragraphs as if they are one paragraph (from the first opening tag until the last closing tag without the intermediate paragraphs).问题是它读取所有段落,就好像它们是一个段落一样(从第一个开始标记到最后一个结束标记,没有中间段落)。 Below is an example of the text.下面是一个文本示例。 In this case it match one instead of 3在这种情况下,它匹配一个而不是 3

</w:tr></w:tbl><w:p w:rsidR="00F24C60" w:rsidRDefault="00F24C60" w:rsidP="009D46A1"><w:pPr><w:spacing w:before="240" w:after="240"/></w:pPr><w:r><w:t></w:t></w:r></w:p><w:p w:rsidR="00F24C60" w:rsidRDefault="00F24C60" w:rsidP="009D46A1"><w:pPr><w:spacing w:before="240" w:after="240"/></w:pPr><w:r><w:t></w:t></w:r></w:p><w:p w:rsidR="00346D4D" w:rsidRPr="00AC7B53" w:rsidRDefault="00F24C60" w:rsidP="009D46A1"><w:pPr><w:spacing w:before="240" w:after="240"/></w:pPr><w:r><w:t></w:t></w:r></w:p><w:tbl><w:tblPr>

Any help to make it match each paragraph alone (3 paragraphs)?有什么帮助让它单独匹配每个段落(3段)?

Thanks.谢谢。

I think, you can't, because you want to create groups inside another tags, but regex don't know about structures it just looking string from begin to end, assume string: eabcabce if need all abc groups I can do next (abc) , however I can't tell that I want all abc groups between e .我认为,你不能,因为你想在另一个标签中创建组,但正则表达式不知道它只是从头到尾查看字符串的结构,假设 string: eabcabce如果需要我接下来可以做的所有 abc 组(abc) ,但是我不能说我想要e之间的所有abc组。

You can use some xml parser.您可以使用一些 xml 解析器。

You can try two regexes for this particular case:对于这种特殊情况,您可以尝试两个正则表达式:

  1. Get content of tbl tag with your regex使用您的正则表达式获取 tbl 标签的内容在此处输入图像描述
  2. Get groups from tbl content with this regex (<w:p [^>]*>.*?<\/w:p>)使用此正则表达式从tbl内容中获取组(<w:p [^>]*>.*?<\/w:p>) 在此处输入图像描述

some links:一些链接:

  1. why not to parse html with regex (I think your xml is close to html:)) RegEx match open tags except XHTML self-contained tags为什么不用正则表达式解析 html (我认为您的 xml 接近html :))
  2. https://www.regextester.com/ https://www.regextester.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM