【问题标题】:Regex to get the text between two text boundaries and the boundaries too正则表达式获取两个文本边界和边界之间的文本
【发布时间】:2012-12-17 07:30:56
【问题描述】:

基于这个问题:Regular expression to extract text between square brackets

this is a [sample] string with [some] special words. [another one]

我还需要提取 [sample] 和 [some] 之间的文本以及两个边界。换句话说,我想要将 [sample] 字符串与 [some]

匹配的正则表达式

【问题讨论】:

    标签: php regex


    【解决方案1】:

    试试这个:

    /\[sample\].*?\[some\]/
    

    您只需要转义方括号并在标记之间使用惰性匹配。

    here on Regexr

    如果文本可以在多行上,则需要另外使用s修饰符启用dotall模式:

    /\[sample\].*?\[some\]/s
    

    这使得. 也匹配换行符,这是默认情况下不做的。

    here on Regexr

    【讨论】: