正则表达式 - PHP Lookaround
我有一个字符串,例如:
$foo = 'Hello __("How are you") I am __("very good thank you")'
我知道这是一个奇怪的字符串,但请留在我身边 :P
我需要一个正则表达式来查找 __("Look for content here") 之间的内容 并将其放入数组中。
即正则表达式会找到“你好吗”和“非常好,谢谢”。
I have a string, such as this:
$foo = 'Hello __("How are you") I am __("very good thank you")'
I know it's a strange string, but stay with me please :P
I need a regex expression that will look for the content between __("Look for content here")
and put it in an array.
i.e. the regular expression would find "How are you" and "very good thank you".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
这意味着:
编辑
正如格雷格提到的:对于不太熟悉环视的人来说,将它们排除在外可能会更具可读性。然后匹配所有内容:
__("
, the string and")
并包装与 string 匹配的正则表达式,.*?
,位于括号内以仅捕获这些字符。然后,您需要通过$matches[1]
获取匹配项。演示:Try this:
which means:
EDIT
And as Greg mentioned: someone not too familiar with look-arounds, it might be more readable to leave them out. You then match everything:
__("
, the string and")
and wrap the regex that matches the string,.*?
, inside parenthesis to capture only those characters. You will then need to get your matches though$matches[1]
. A demo:如果您想使用 Gumbo 的建议,则应归功于他的模式:
请确保对结果使用
$matches[1]
,除非您也想要完整的字符串结果。$matches
的var_dump()
:If you want to use Gumbo's suggestion, credit goes to him for the pattern:
Make sure to use
$matches[1]
for your results unless you want the full string results too.var_dump()
of$matches
: