简体   繁体   中英

Make this Regex work with negative lookahead

I'm doing an html and bbcode parser. I have this regex that catches repeated groups from the inside-out:

$re = '/<b>((?:(?!<\/?b>).)*)<\/b>/is';

But I want to be able to match <b class=”string”>text</b> or any other attribute inside. I was already doing it by using:

'/<b((\s)+?.*?)?\>(.*?)<\/b>/is'

But now when trying to join them and add the new negative lookahead, I cannot make it work.

I tried '/<b((\\s)+?.*?)?\\((?:(?!</?b((\\s)+?.*?)?>).)*)<\\/b>/is' but does not work properly, for this:

<b class=”string2”><b class=”string”>text</b></b>

It matches from the first b tag, and it shouldn't. I would like to get:

<b class=”string2”>[b]text[/b]</b>

This will all < b > with [b]:

<?php

$str = '<b>test</b><b class=”string2”><b class=”string”>text</b></b>';
$prev = '';
while ($prev != $str) {
    $prev = $str;
    $str = preg_replace("/<b[ a-z0-9\"'\=”]*?>(.*?)<\/b>/is","[b]$1[/b]",$str);
}
echo $str;

?>

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