简体   繁体   中英

PHP Regex - negate a match if it contains a word (string)

I've tried many ways to do this, the only one which seems to partially do what I want is the lookahead negation described in String negation using regular expressions and How to negate specific word in regex? but I can't get it to work with my current regex.

Assuming I have this string:

[url=http://www.example.com]this url BB tag should match[/url]
[url=http://www.example.com][spoiler]this url BB tag shouldn't match[/spoiler][/url]

Edit: More in-depth string which shouldn't return a match for debugging:

[url=http://www.example.com]Lorem[spoiler]this url BB tag shouldn't match[/spoiler]Ipsum[/url]

And the current regex pattern:

#\[url=([\w]+?://.*?)\]([^?\n\r\t].*?)\[/url\]#is

Edit: I'm using the following regex (without # and #is) to debug it on Regex Buddy:

\[url=([\w]+?://.*?)\]([^?\n\r\t].*?)\[/url\]

(these 2 regex should have the same effect on my application as I can easily adapt it, so work with the latter if it makes more sense)

Will match both lines in the string. I want it to don't return a match when there's a [spoiler] string between the [url=xxxx://yyyy] and [/url] .

I think it can be done with the negative lookahead but I can't seem to find a way to insert it in the current regex without turning it unusable.

Any help is appreciated.

这应该可以解决问题。

'#\[url=([^\]]+)\](?!\[spoiler\])(.*?)\[/url\]#'

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