Flu*_*key 4 php regex preg-match-all
我有一个字符串,例如:
$foo = 'Hello __("How are you") I am __("very good thank you")'
Run Code Online (Sandbox Code Playgroud)
我知道这是一个奇怪的字符串,但请跟我一起:P
我需要一个正则表达式来查找__("在这里查找内容")之间的内容并将其放在一个数组中.
即正则表达式会找到"你好吗"和"非常好,谢谢你".
试试这个:
preg_match_all('/(?<=__\(").*?(?="\))/s', $foo, $matches);
print_r($matches);
Run Code Online (Sandbox Code Playgroud)
意思是:
(?<= # start positive look behind
__\(" # match the characters '__("'
) # end positive look behind
.*? # match any character and repeat it zero or more times, reluctantly
(?= # start positive look ahead
"\) # match the characters '")'
) # end positive look ahead
Run Code Online (Sandbox Code Playgroud)
编辑
而正如格雷格所说:有些人不太熟悉环顾四周,将它们排除在外可能更具可读性.然后,您匹配的一切:__("
中,串并")
,并包裹了相匹配的正则表达式的字符串,.*?
,括号内为仅捕捉这些字符.然后你需要得到你的比赛$matches[1]
.演示:
preg_match_all('/__\("(.*?)"\)/', $foo, $matches);
print_r($matches[1]);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1590 次 |
最近记录: |