简体   繁体   English

使此正则表达式可以提前否定地工作

[英]Make this Regex work with negative lookahead

I'm doing an html and bbcode parser. 我正在做一个html和bbcode解析器。 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. 但我希望能够匹配<b class=”string”>text</b>或内部的任何其他属性。 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((\\s)+?.*?)?\\((?:(?!</?b((\\s)+?.*?)?>).)*)<\\/b>/is'但不能正常运行,为此:

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

It matches from the first b tag, and it shouldn't. 它与第一个b标签匹配,但不匹配。 I would like to get: 我想得到:

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

This will all < b > with [b]: 所有<b>将带有[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;

?>

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

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