包含至少 2 个空格的单词的匹配字符串


Matching strings that contain words with at least 2 spaces

我需要匹配包含至少 2 个空格的单词的字符串。我将如何在正则表达式中做到这一点?

$content = "one two three";
preg_match_all("~(['w]+(?:['s]))+~", $content, $match); 
print_r($match);

结果:

Array ( [0] => Array ( [0] => one two ) [1] => Array ( [0] => two ) )

需要结果:

Array ( [0] => Array ( [0] => one two three))

P:S

请记住,匹配的字符串必须包含至少 2 个空格,并且字符串只能包含单词和空格 - 如果字符串中的空格少于 2 个,或者字符串中是否有任何其他字符不是单词或空格,则不应匹配。

试试这个

^(?=(?:.*?'s){2})[a-zA-Z ]+$

演示

解释:
(?=(?:.*?'s){2}) 向前看至少检查 2 个空格。
[a-zA-Z ] 仅匹配单词和空格。