dongpu2476 2014-05-17 19:28
浏览 23
已采纳

不匹配BBCode标记之间的URL

As the title says I want my pattern to match the URL's in the string, except the ones that are between the BBCode tags.

So far I have made a pattern that doesn't match the URL's between the [img] tags, but have no enough regex skills to make it work for all tags like the more advanced onces ([url=xxx]yyy[/url] and such). So to be clear: nothing between [ and ] should match.


Here is my working example to not match the URL's between the [img] tag:

http://regexr.com/v1?38mae (may have to paste the below pattern due to encoding being messed up)

Pattern:

(?<!\[img])(((http|ftp|https):\/\/)|www\.)[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#!]*[\w\-\@?^=%&/~\+#])

I'd appreciate any kind of help!

  • 写回答

1条回答 默认 最新

  • duanla8800 2014-05-17 19:36
    关注

    You can add this at the begining of your pattern:

    \[[^]]*](*SKIP)(*FAIL)|
    

    This subpattern will find all that is between square brackets, make the pattern fail and force to not retry the substring.

    Example with your pattern (with ~ as delimiter):

    $pattern = '~\[[^]]*](*SKIP)(*FAIL)|
                 (?<!\[img])
                 (?:(?:ht|f)tps?://|www\.)
                 [\w-]+
                 (?:\.[\w-]+)+
                 [\w-.,@?^=%&:/\~+#!]*[\w-@?^=%&/\~+#]~xiu';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?