修改此正则表达式:{([^ \]] *)}接受\]

时间:2012-03-06 14:08:44

标签: php regex escaping pcre

我有这个正则表达式:

{([^\]]*)} // 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

希望它有意义

2 个答案:

答案 0 :(得分:7)

您可以使用替换:

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

答案 1 :(得分:0)

我会选择:

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

与Marks回答不同,这可以将[\\]]解析为\\,这样您就可以使用\作为通用转义字符,而不仅仅是\]

相关问题