[英]How to match all @__('...') on string in php regex?
我有以下字符串,需要匹配字符串中的所有@__('...')
或@__("...")
。
$string = <<<EOD
@__('test1') @__('test2 (foo)') @__('test\'3') @__("test4")
@__('test5')
EOD;
預期的:
test1
test2 (foo)
test\'3
test4
test5
嘗試了一些模式:
這種模式只有在一行只有一個目標字符串時才能匹配:
preg_match_all("/@__\([\'\"](.+)[\'\"]\)/", $string, $matches);
dd($matches);
array:2 [▼
0 => "test1') @__('test2 (foo)') @__('test\'3') @__("test4"
1 => "test5"
]
下面的不能匹配包含)
的字符串:
preg_match_all("/@__\(['|\"]([^\'][^\)]+)['|\"]\)/", $string, $matches);
dd($matches);
array:4 [▼
0 => "test1"
1 => "test\'3"
2 => "test4"
3 => "test5"
]
提前致謝。
我找到了解決方案,只需添加?
.+
之后:
preg_match_all("/@__\([\'\"](.+?)[\'\"]\)/", $string, $matches);
謝謝巴爾瑪
使用非貪婪量詞,以便獲得最短匹配,而不是最長匹配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.