doudong7256 2012-04-18 03:08
浏览 44
已采纳

PHP Regex - 如果包含单词(字符串)则取消匹配

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]+?://.*?)\]([^?
\t].*?)\[/url\]#is

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

\[url=([\w]+?://.*?)\]([^?
\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.

  • 写回答

1条回答 默认 最新

  • dsbx40787736 2012-04-18 03:22
    关注

    This should solve the problem.

    '#\[url=([^\]]+)\](?!\[spoiler\])(.*?)\[/url\]#'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起