简体   繁体   English

正则表达式匹配一个匹配组中的多行

[英]Regex matching multiple lines in one match group

I've got the following string:我有以下字符串:

<div>Some headline - list 1<br />
&gt; List item 1<br />
&gt; List item 2<br />
&gt; List item 3<br />
<br />
List 2<br />
&gt; foo<br />
&gt; bar<br />
&gt; baz<br />
<br />
&nbsp;</div>

It contains two maleformed lists.它包含两个畸形列表。 It is my goal to wrap the items of these lists into <ul> .我的目标是将这些列表的项目包装到<ul> To accomplish that, I am looking for a regex that matches all Items of each list into one match group.为了实现这一点,我正在寻找一个将每个列表的所有项目匹配到一个匹配组的正则表达式。

So my expected result would be:所以我的预期结果是:

&gt; List item 1<br />
&gt; List item 2<br />
&gt; List item 3<br />

and

&gt; foo<br />
&gt; bar<br />
&gt; baz<br />

All regex magic I tired failed because they did match only a single line of the list, or the match group was including all the content between the beginning of the first list and the end of the last one.我厌倦的所有正则表达式魔术都失败了,因为它们只匹配了列表的一行,或者匹配组包含了第一个列表的开头和最后一个列表的结尾之间的所有内容。

I've tried ~&gt; (.*)<br />\\s~gis我试过了~&gt; (.*)<br />\\s~gis ~&gt; (.*)<br />\\s~gis which matches both of the lists. ~&gt; (.*)<br />\\s~gis匹配两个列表。

You can try this regex, To capture only the Lists.,你可以试试这个正则表达式,只捕获列表。,

Im using java regex.,我使用java正则表达式。,

(&gt; .*(?:<br\s/>\s+List\s2<br\s/>)?)

It captures these elements.,它捕获了这些元素。,

&gt; List item 1<br />
&gt; List item 2<br />
&gt; List item 3<br />
&gt; foo<br />
&gt; bar<br />
&gt; baz<br />

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

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