在正则表达式中使用 Lookahead 和 Lookbehind 忽略 BBCode 之间任意位置的单词 [英] Using Lookahead and Lookbehind in regex to ignore a word anywhere between a BBCode
问题描述
我希望在此处扩展特定代码:
I'm looking to expand on a particular code here:
/(?<![@#]|(\[img\]))\b(".str_replace(" ", "[\-_ ]", $key).")(?!\[\/img\])\b/i
目前,它检测 @
或 #
是否直接在有问题的 $key
后面(这很好),或者 >[img]
或 [/img]
直接在 $key
之前/之后(有问题).我想添加一个通配符,以便 [img]
和 [/img]
之间的 $key
ANYWHERE 不会被替换,同时仍然保持 @
或 #
必须仍然直接位于 $key
后面的事实.我知道在后视中不允许使用通配符.
Currently, it detects whether @
or #
is directly behind the $key
in question (which is fine), OR whether [img]
or [/img]
is directly before/after the $key
(a problem). I want to add a wildcard so that the $key
ANYWHERE in between [img]
and [/img]
will not be replaced, while still keeping the fact that @
or #
must still be directly behind the $key
. I am aware that wildcards are not allowed in lookbehind.
这可能吗?
我有点误解了我自己的代码.我意识到即使 [img]
不在单词前面,[/img]
仍然会触发,从而允许 @BLUE[/img]
> 不触发.我希望将 #/@
和 [img][/img]
之间的情况分开.这方面的帮助也将大有帮助.
I misinterpreted my own code a bit. I realized that [/img]
will still trigger even if [img]
doesn't precede the word, thus allowing @BLUE[/img]
to not trigger. I wish to separate the cases between #/@
and [img][/img]
. Assistance on this will greatly help as well.
基本上,[img]
和 [/img]
中的所有内容都会忽略 $key
的 preg_replace
、@$key
和 #$key
.然而,即使作为独立的 @$key
和 #$key
(没有 [img]
标签),$key
不应更换.
Basically, everything within [img]
and [/img]
will ignore preg_replace
of the $key
, @$key
, and #$key
. However, even as a standalone @$key
and #$key
(without [img]
tags), $key
should not be replaced.
推荐答案
使用环视并不是一个好方法,因为你不能使用可变长度的环视.
Using lookarounds is not a good way to do that since you can't use a variable length lookbehind.
目标是跳过 [img]
标签之间的内容,让我们看看:
The goal is to skip content between [img]
tags, lets see a way:
$result = preg_replace('~\[(img|url)].*?\[/\1](*SKIP)(*FAIL)|(?<![@#])\bHELLO\b~s',
'GOODBYE', $str);
(*SKIP)
禁止在右侧子模式失败的情况下重试左侧已匹配的字符串部分.
(*SKIP)
forbids to retry the part of the string that has been matched on the left if the subpattern fails on the right.
(*FAIL)
强制模式失败.
因为 [img]
标签总是首先被模式的第一个分支尝试,所以模式的第二个分支总是匹配 [img]
之外的字符串部分代码>标签.
Since [img]
tags are always tried first by the first branch of the pattern, the second branch of the pattern matches always parts of the string that are outside [img]
tags.
另一种方式
您可以将您搜索的关键字描述为前面有几个[img]..[/img]
和其他不是[img]
的字符的词标签或关键词:
you can describe the key you search as a word that is preceded by several [img]..[/img]
and other characters that are not [img]
tags or the key word:
$pattern = <<<'LOD'
~
(?> # atomic group: all possible content before "HELLO"
(?> # other characters
[^[H]++ # all characters except [ and H
| # OR
\[(?!img]|url]) # a [ not followed by img or url
| # OR
\BH # an H not preceded by a word boundary
| # OR
H(?!ELLO\b) # an H not followed by ELLO and a word boundary
)+
| # OR
\[(img|url)].*?\[/\1] # img or url tags
)*
\K # resets all from the match result
(?<![@#])HELLO
~sx
LOD;
这篇关于在正则表达式中使用 Lookahead 和 Lookbehind 忽略 BBCode 之间任意位置的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!