正则表达式匹配两个字符串,包括那些字符串

时间:2014-01-15 22:03:22

标签: regex

示例字符串:

{{--
    some text
--}}

我正在尝试匹配{{ - 包括第一个 - }之间的任何内容 它还需要捕获回报和换行符。

我试过这样的事情:\{\{--[^\r\n]--\}\}它似乎捕捉了括号之间的所有内容,但我也无法弄清楚如何捕捉括号。

修改 我正在尝试修改一个sublime文本插件,为laravel的刀片模板添加语法hilighting。如下所述:'({{ - [\ s \ S] * - }})'匹配我想要匹配的内容。括号可能被不同的规则所覆盖。

1 个答案:

答案 0 :(得分:2)

您可以使用此正则表达式:

(\{\{--[\s\S]*--\}\})

在线演示:http://regex101.com/r/mT1jT4

<强>解释

1st Capturing group (\{\{--[\s\S]*--\}\})
\{ matches the character { literally
\{ matches the character { literally
-- matches the characters -- literally
[\s\S]* match a single character present in the list below
Quantifier: Between zero and unlimited times, as many times as possible,
            giving back as needed [greedy]
\s match any white space character [\r\n\t\f ]
\S match any non-white space character [^\r\n\t\f ]
-- matches the characters -- literally
\} matches the character } literally
\} matches the character } literally