首页 文章

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

提问于
浏览
1

示例字符串:

{{--
    some text
--}}

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

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

edit I 'm trying to modify a sublime text plugin that adds syntax hilighting for laravel' s刀片模板 . 如下所述:'({{--[\s\S]*--}})'匹配我想要匹配的内容 . 括号可能被不同的规则所覆盖 .

1 回答

  • 1

    你可以使用这个正则表达式:

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

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

    Explanation:

    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
    

相关问题