C#正则表达式匹配并包装多行

C#正则表达式匹配并包装多行,c#,regex,C#,Regex,我需要制作这样的文本,例如 Founded in 2008, Stack Overflow sees 40 million visitors each month || <b>ID</b> || <b>Column1</b> || <b>Column2</b> || | | | | Stack Overflow Documentation, the largest content expansion since Q&am

我需要制作这样的文本,例如

Founded in 2008, Stack Overflow sees 40 million visitors each month

|| <b>ID</b> || <b>Column1</b> || <b>Column2</b> ||
| | | |

Stack Overflow Documentation, the largest content expansion since Q&A, launches in July

|| <b>Name</b> || <u>Surname</u> || <u>DoB</u> ||
| | | |

The Developer Story launches in October, giving developers a better way to present their skills
但这不是我需要的,它选择了错误的区域,你可以在这里看到

另一个尝试是这样的

    Founded in 2008, Stack Overflow sees 40 million visitors each month

<span>|| <b>ID</b> || <b>Column1</b> || <b>Column2</b> ||
| | | |</span>

Stack Overflow Documentation, the largest content expansion since Q&A, launches in July

<span>|| <b>Name</b> || <u>Surname</u> || <u>DoB</u> ||
| | | |
| | | |
</span>

The Developer Story launches in October, giving developers a better way to present their skills
((^|\r\n{2,}|)+(\|{1,2}))(.*)(\|{1,2}(\r\n{2,}|$|)+)
|| A | B |
|| c | d |
Stack Overflow Documentation, the largest content expansion since Q&A, launches in July
<span>
|| <b>Name</b> || <u>Surname</u> || <u>DoB</u> ||
| | | |

<!-- not suppose to be wraped up -->
</span><span>||


| a | b | c | u |

</span>The Developer Story launches in October, giving developers a better way to present their skills
<span>
| a | b | c |
| d | e | f |</span>
但它最终选择了每一行,您可以在这里看到示例

我应该如何更改正则表达式以使其以正确的方式工作

UPD

Wiktor Stribiżew(感谢他)在评论中告诉我尝试他的例子,它在上面的例子中效果很好,但并非适用于所有可能的情况(这里的例子)

所谓的表可以是这样的

    Founded in 2008, Stack Overflow sees 40 million visitors each month

<span>|| <b>ID</b> || <b>Column1</b> || <b>Column2</b> ||
| | | |</span>

Stack Overflow Documentation, the largest content expansion since Q&A, launches in July

<span>|| <b>Name</b> || <u>Surname</u> || <u>DoB</u> ||
| | | |
| | | |
</span>

The Developer Story launches in October, giving developers a better way to present their skills
((^|\r\n{2,}|)+(\|{1,2}))(.*)(\|{1,2}(\r\n{2,}|$|)+)
|| A | B |
|| c | d |
Stack Overflow Documentation, the largest content expansion since Q&A, launches in July
<span>
|| <b>Name</b> || <u>Surname</u> || <u>DoB</u> ||
| | | |

<!-- not suppose to be wraped up -->
</span><span>||


| a | b | c | u |

</span>The Developer Story launches in October, giving developers a better way to present their skills
<span>
| a | b | c |
| d | e | f |</span>
或者

| a | b | c |
| d | e | f |
UPD2

那是一个很近的房间,但它有一个难看的空房间

UPD3

这个是close(),但是对于这个测试文本

Stack Overflow Documentation, the largest content expansion since Q&A, launches in July

|| <b>Name</b> || <u>Surname</u> || <u>DoB</u> ||
| | | |


||


| a | b | c | u |

The Developer Story launches in October, giving developers a better way to present their skills

| a | b | c |
| d | e | f |
将被解析为html,看起来像表,其中,
| | Cell | |
表示标题,
| Cell |
表示常规单元格

所以,在分析之后,它看起来像

<table>
  <tr>
    <th>ID</th>
    <th>Column1</th>
    <th>Column2</th>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>

身份证件
专栏1
专栏2
正则表达式是

(\|\|?([^|\n\r]+\|\|?)+($|[\r\n]+))+
匹配组为
$0
()

其工作原理如下:

(
  \|\|?         #the line starts with one or two pipes
  (
    [^|\n\r]+   #followed by at least one non-pipe characther
    \|\|?       #and the cell endt with one or two pipes
  )+            #at least one cell, otherwise even the line "||" would be matched
  (
    $           #the text ends (you are NOT in multiline mode) 
  |
    [\r\n]+     #or [\r\n] characters are matched (at least one, otherwise would match even "||A|B"), in order to match also the possible following line
  )
)+              #at least one line
如果不想匹配“table”后面的空格/新行,只需使用一个稍难的正则表达式():


在最后一个正则表达式中,请记住使用
m
标志。

好吧,您的预期输出似乎不正常,是否需要复制
| | |
?试试看,是的,好像是空的cells@DanilGholtsman这实际上是什么意思“是的,它就像一排空单元格”?哦,对不起,我会更新问题来解释that@WiktorStribiżew cool,它可以工作,我更新了它(因为它可以以
|
以及
|
开始或结束)