正则表达式 - PHP Lookaround

发布于 2024-08-24 11:13:52 字数 240 浏览 3 评论 0原文

我有一个字符串,例如:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感情废物 2024-08-31 11:13:52

试试这个:

preg_match_all('/(?<=__\(").*?(?="\))/s', $foo, $matches);
print_r($matches);

这意味着:

(?<=     # 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

编辑

正如格雷格提到的:对于不太熟悉环视的人来说,将它们排除在外可能会更具可读性。然后匹配所有内容:__(", the string and ") 并包装与 string 匹配的正则表达式, .*?,位于括号内以仅捕获这些字符。然后,您需要通过 $matches[1] 获取匹配项。演示:

preg_match_all('/__\("(.*?)"\)/', $foo, $matches);
print_r($matches[1]);

Try this:

preg_match_all('/(?<=__\(").*?(?="\))/s', $foo, $matches);
print_r($matches);

which means:

(?<=     # 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

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:

preg_match_all('/__\("(.*?)"\)/', $foo, $matches);
print_r($matches[1]);
赏烟花じ飞满天 2024-08-31 11:13:52

如果您想使用 Gumbo 的建议,则应归功于他的模式:

$foo = 'Hello __("How are you")I am __("very good thank you")';

preg_match_all('/__\("([^"]*)"\)/', $foo, $matches);

请确保对结果使用 $matches[1] ,除非您也想要完整的字符串结果。

$matchesvar_dump()

array
  0 => 
    array
      0 => string '__("How are you")' (length=16)
      1 => string '__("very good thank you")' (length=25)
  1 => 
    array
      0 => string 'How are you' (length=10)
      1 => string 'very good thank you' (length=19)

If you want to use Gumbo's suggestion, credit goes to him for the pattern:

$foo = 'Hello __("How are you")I am __("very good thank you")';

preg_match_all('/__\("([^"]*)"\)/', $foo, $matches);

Make sure to use $matches[1] for your results unless you want the full string results too.

var_dump() of $matches:

array
  0 => 
    array
      0 => string '__("How are you")' (length=16)
      1 => string '__("very good thank you")' (length=25)
  1 => 
    array
      0 => string 'How are you' (length=10)
      1 => string 'very good thank you' (length=19)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文