简体   繁体   中英

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> . 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 which matches both of the lists.

You can try this regex, To capture only the Lists.,

Im using java regex.,

(&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 />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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