2

我不是正则表达式专家,但几个小时后,我构建了这个正则表达式:

#\[url=(?!.*?<div onclick="unveil_spoiler.*?\[/url\])([^_\W]+?://.*?)\](.+?)\[/url\]#i

这是不区分大小写的:

\[url=(?!.*?<div onclick="unveil_spoiler.*?\[/url\])([^_\W]+?://.*?)\](.+?)\[/url\]

匹配[url=xxxx://yyyy]zzzz[/url]模式,除非它包含介于and 和之间的<div onclick="unveil_spoiler字符串。[url=[/url]

\[url.*?\]现在我正在尝试添加一个类似的检查,如果它包含介于\[url=and之间的匹配项,则不返回匹配项\[/url\]。我尝试了很多方法,但我似乎可以找到一个 100% 有效的方法。

首先,我尝试添加另一个与我的正则表达式中已经存在的非常相似的负前瞻,它部分工作,但是看起来前瞻一直持续到行尾 - 直到最后\[/url\]- 对于每场比赛,我想要前瞻\[/url\]像捕获组一样停在第一个。

这是一个用于调试的字符串:

[url=http://www.match.com]Match[/url][url=http://www.nomatch.com<div onclick="unveil_spoiler"]No match[/url][url=http://www.match.com]Match[/url][url=http://www.nomatch.com]<div onclick="unveil_spoiler" No match[/url]
[url=http://www.nomatch.com]No <div onclick="unveil_spoiler"match[/url][url=http://www.match.com]Match[/url][url=http://www.nomatch.com]No <div onclick="unveil_spoiler" match[/url][url=http://www.match.com]Match[/url]

[url=http://www.match.com]Match[/url][url=http://www.match.com][b]Match[/b][/url][url=http://www.match.com]Match[/url][url=http://www.match.com]Match[/url]

[url=http://www.thisshouldntmatch.com[url=http://www.match.com]Match[/url]This shouldn't match[/url]

[url=http://www.thisshouldntmatch.com[url=http://www.thisshouldntmatch.com[url=http://www.match.com]Match[/url]]This shouldn't match[/url]This shouldn't match[/url]

[url=http://www.thisshouldntmatch.com[url=http://www.match.com]Match[/url]This shouldn't match[/url][url=http://www.match.com]Match[/url]

[url=http://www.thisshouldntmatch.com]This shouldn't match[url=http://www.match.com]Match[/url][url=http://www.match.com]Match[/url][/url]

[url=http://www.match.com]Match[/url][url=http://www.match.com]Match[/url][url=http://www.match.com]Match[/url][url=http://www.match.com]Match[/url]

在帖子开头发布正则表达式后,它将完美匹配第一行中的 2 个匹配项。现在我希望它在比赛内部不返回\[url.*?\]比赛,我试过这个正则表达式:

\[url=(?!.*?\[url.*?\].*?\[/url.*?\])(?!.*?<div onclick="unveil_spoiler.*?\[/url\])([^_\W]+?://.*?)\](.+?)\[/url\]

还有这个:

\[url=(?!.*?(?:<div onclick="unveil_spoiler|\[url.*?\]).*?\[/url\])([^_\W]+?://.*?)\](.+?)\[/url\]

当匹配中有一个内部时,它不会返回匹配\[url.*?\],但是它也会停止匹配它应该(并且作为第一个正则表达式)的第一行(在示例字符串中)的第一个匹配。也就是说,它只会匹配每一行的最后一个有效匹配。

我认为这是先行的问题,它不会在一开始就停止\[/url\],有没有办法让它变得懒惰/修复它?

任何帮助表示赞赏。

4

2 回答 2

2

我认为以下应该有效:

\[url=(?:(?!<div onclick="unveil_spoiler"|\[url.*?\].*?\[/url.*?\]).)*?([^_\W]+?://[^\[\]]*)\]((?:(?!\[/?url).)*)\[/url\]

http://rubular.com/r/7h9EJ0casb

于 2012-04-19T04:42:54.663 回答
1

这行得通吗?

\[url=[^\[<]*?\](?:(?!(\[url)|<).)*?\[\/url\]

http://regexr.com?30mna

于 2012-04-19T04:08:43.843 回答