简体   繁体   English

多次匹配正则表达式模式

[英]Match regex pattern multiple times

I'm trying to write a regex that will match a given pattern between 2 and unlimited times.我正在尝试编写一个正则表达式,它将在 2 次和无限次之间匹配给定的模式。 Effectively I'm just trying to combine multiple line breaks in to one, if any exist at all.实际上,我只是想将多个换行符合并为一个,如果有的话。

Sample input:样本输入:

<br><br>
<br>
ABC
<br>
<br>

Expected output:预期 output:

<br>
ABC
<br>

If the regex was run on the above output then I would expect to see the exact same output.如果正则表达式在上述 output 上运行,那么我希望看到完全相同的 output。

There could be any amount of whitespace between the <br> tags. <br>标签之间可以有任意数量的空格。

What I've tried:我试过的:

$html = preg_replace('/(?:<br>\s?){2,}/s', null, $html);

Just look for a <br> followed by optional space characters 2 or more times and replace with <br> :只需查找<br>后跟可选空格字符 2 次或更多次并替换为<br>

$html = preg_replace('/(<br>\s*){2,}/', '<br>', $html);

You might replace with "<br>\n" if you want.如果需要,您可以替换为"<br>\n"

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

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