vBulletin的复杂(?)名称匹配正则表达式 [英] Complex(?) Name Matching Regex for vBulletin
问题描述
我正在为论坛创建一些自定义BBcode.我正在尝试使正则表达式正确,但是已经使我迷恋了好几个星期.任何专家的建议都欢迎.
I'm creating some custom BBcode for a forum. I'm trying to get the regular expression right, but it has been eluding me for weeks. Any expert advice is welcome.
样本输入(一个非常基本的示例):
Sample input (a very basic example):
[quote=Bob]I like Candace. She is nice.[/quote]
Ashley Ryan Thomas
本质上,我想将[user] [/user] BBcode中的任何名称(从指定列表中)括起来...,当然,除了那些被引用的名称,因为这样做会导致一些严重的解析错误.
Essentially, I want to encase any names (from a specified list) in [user][/user] BBcode... except, of course, those being quoted, because doing that causes some terrible parsing errors.
所需的输出:
[quote=Bob]I like [user]Candace[/user]. She is nice.[/quote]
[user]Ashley[/user] [user]Ryan[/user] [user]Thomas[/user]
我当前的代码:
$searchArray = array(
'/(?i)([^=]|\b|\s|\/|\r|\n|\t|^)(Ashley|Bob|Candace|Ryan|Thomas)(\s|\r|\n|\t|,|\.(\b|\s|\.|$)|;|:|\'|"|-|!|\?|\)|\/|\[|$)/'
);
$replaceArray = array(
"\\1[user]\\2[/user]\\3"
);
$text = preg_replace($searchArray, $replaceArray, $input);
当前产生的内容:
[quote=Bob]I like [user]Candace[/user]. She is nice.[/quote]
[user]Ashley[/user] Ryan [user]Thomas[/user]
请注意,[用户]标签未封装Ryan.另请注意,许多额外的正则表达式匹配字符是在论坛上出现时按需添加的,因此删除它们只会使它在其他情况下不匹配(即,不可以).当然,除非您发现正则表达式本身存在明显错误,在这种情况下,请务必指出.
Notice that Ryan isn't encapsulated by [user] tags. Also note that much of the additional regex matching characters were added on an as-needed basis as they cropped up on the forums, so removing them will simply make it fail to match in other situations (i.e. a no-no). Unless, of course, you spot a glaring error in the regex itself, in which case please do point it out.
尽管如此,任何帮助将不胜感激!谢谢.
Really, though, any assistance would be greatly appreciated! Thank you.
推荐答案
很简单,就是在搜索名称的两端匹配定界符(\s|\r|...)
.较差的Ashley
和Ryan
在测试字符串中共享一个空格字符.但是正则表达式只能匹配一次-左边界或右边界.
It's quite simply that you are matching delimiters (\s|\r|...)
at both ends of the searched names. The poor Ashley
and Ryan
share a single space character in your test string. But the regex can only match it once - as left or right border.
这里的解决方案是使用断言.用(?<= )
括住左列表,用(?= )
括住右列表,使它们成为:
The solution here is to use assertions. Enclose the left list in (?<= )
and the right in (?= )
so they become:
(?<=[^=]|\b|\s|\/|^)
(?=\s|,|\.(\b|\s|\.|$)|;|:|\'|"|-|!|\?|\)|\/|\[|$)
顺便说一句,\s
已经包含\r|\n|\t
,因此您可以删除它.
Btw, \s
already contains \r|\n|\t
so you can probably remove that.
这篇关于vBulletin的复杂(?)名称匹配正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!