如何破译正则表达式?

时间:2019-05-23 14:40:01

标签: php regex string preg-match-all regex-negation

preg_match_all('/({\$([^}]+)})/', $result, $matches)

有人可以帮助解释此通话中的正则表达式吗?

正则表达式不是我的最佳选择。据我所知,它正在寻找看起来像{$foo}的比赛,但我认为我缺少什么?

1 个答案:

答案 0 :(得分:0)

在这里,我们有一个非常简单的表达式,它传递了几乎所有字符,从左至右用<v-checkbox v-model="devText1" true-value="Yes" false-value="No"/> {并从右至$来限定。它收集介于以下之间的几乎所有字符:

enter image description here

DEMO

RegEx

如果不需要此表达式,则可以在regex101.com中对其进行修改或更改。例如,如果我们愿意,我们可以限制它并失败特殊字符:

}

DEMO

enter image description here

RegEx电路

jex.im可视化正则表达式:

enter image description here

演示

({\$\w+})

PHP测试

const regex = /({\$\w+})/gm;
const str = `{\$foo}
{\$foo_090_}
{\$foo_@#\$%_5678}
{\$foo_@#\$%_{+[]:}`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}