【问题标题】:php regex finding double bracketsphp正则表达式查找双括号
【发布时间】:2015-10-13 03:01:32
【问题描述】:

我提交了一些文本,它将包含双括号中的单词,但有些只能在单括号中。但我只想要双括号中的。

例子:

[[test]] [test1] [[test2]] [test5] [[test3]]

与其他词等在同一字符串中...

例如:

[[test]]快速的[test1]棕色[[test2]]狐狸[[test3]]跳过了[mytest]懒惰的[[test4]]狗

现在我有一个 preg_match_all :

(preg_match_all('/\\[.*?\\]\\]/is',$str,$match))

但是,它也会拉出单括号......我很讨厌正则表达式。

有人对我如何只拉双括号有任何意见吗?

谢谢!

【问题讨论】:

    标签: php regex preg-match-all


    【解决方案1】:

    改用这个。

    \[{2}.*?\]{2}
    

    演示:https://regex101.com/r/bW0mM6/1

    {2} 需要出现 2 次。你只用你的表达式检查一个括号。

    PHP 演示:http://sandbox.onlinephpfunctions.com/code/2f16f7e102d822e31dc128a384f8090181c7461d

    PHP 用法:

    $str = '[[test]] [test1] [[test2]] [test5] [[test3]]';
    preg_match_all('/\[{2}(.*?)\]{2}/is',$str,$match);
    print_r($match[1]);
    

    输出:

    Array
    (
        [0] => test
        [1] => test2
        [2] => test3
    )
    

    另请注意,我在 PHP 演示中捕获了双括号值,不确定这是否是您想要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多