例如:
$s1 = "Test Test the rest of string"
$s2 = "Test the rest of string"
Run Code Online (Sandbox Code Playgroud)
我想积极匹配,$s1但不是$s2,因为第一个词$s1与第二个相同.Word 'Test'就是例子,正则表达式应该适用于任何单词.
if(preg_match('/^(\w+)\s+\1\b/',$input)) {
// $input has same first two words.
}
Run Code Online (Sandbox Code Playgroud)
说明:
^ : Start anchor
( : Start of capturing group
\w+ : A word
) : End of capturing group
\s+ : One or more whitespace
\1 : Back reference to the first word
\b : Word boundary
Run Code Online (Sandbox Code Playgroud)