我遇到了匹配[*]的问题,有时会出现这种问题,有时候不会。任何人都有建议?PHP - Preg_match_all可选匹配
$name = 'hello $this->row[today1][] dfh fgh df $this->row[test1] ,how good $this->row[test2][] is $this->row[today2][*] is monday';
echo $name."\n";
preg_match_all('/\$this->row[.*?][*]/', $name, $match);
var_dump($match);
输出: 你好$这 - >行[测试],多好$这个 - >行[TEST2]是>行[日] [*]为$这个 - 周一
array (
0 =>
array (
0 => '$this->row[today1][*]',
1 => '$this->row[test1] ,how good $this->row[test2][*]',
2 => '$this->row[today2][*]',
),
)
现在[0] [1]匹配会过多,因为它匹配到下一个'[]',而不是以'$ this-> row [test]'结尾。我猜[*] /添加了一个通配符。不知何故需要检查下一个字符是否[在匹配[]之前]。任何人?
感谢
尝试'/(\ $这个 - >行\(\ [^ \)] * \)) /' – Orangepill
试试这个,preg_match_all('/\$this->row(\[.*?'])(\[\*\])?/',$ name,$ match); – Nightmare
这两个不匹配 – Karassik