【问题标题】:Modify this regex: {([^\]]*)} to accept \]修改这个正则表达式: {([^\]]*)} 接受 \]
【发布时间】:2012-03-24 00:33:33
【问题描述】:

我有这个正则表达式:

{([^\]]*)} // any character except ']' but i want it to accept also '\]' this combination of 2chars

例子

'Lorem Ipsum is simply] dummy text' should return => 'Lorem Ipsum is simply' (and this ones does) but
'Lorem Ipsum is simply\] dummy text' => should return all the text because the ']' is escaped

希望有意义

【问题讨论】:

    标签: php regex escaping pcre


    【解决方案1】:

    我会选择:

    '~(?:[^\\\\\\]]*|\\\\.)*~' // (?:[^\\\]]|\\.)*
    

    与 Marks 答案不同,它能够将 [\\]] 解析为 \\,这将使您能够使用 \ 作为通用转义字符,而不仅仅是 \]

    【讨论】:

      【解决方案2】:

      您可以使用替代:

      (?:\\\]|[^\]])*
      

      【讨论】:

        猜你喜欢
        • 2013-09-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多