匹配包含至少 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 space 秒。
[a-zA-Z ]
只匹配单词和 space.
我需要匹配包含至少 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 space 秒。
[a-zA-Z ]
只匹配单词和 space.