Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何只匹配由一个或多个空格分隔的两个单词?
[\w]+[\s]+[\w]+
火柴:
one two one two three //but this should not match as it countains more than 2 words
您可以将此正则表达式与行开始和结束锚点一起使用:
^\\s*\\w+\\s+\\w+\\s*$
注意使用^and$确保只有two word string, 用空格分隔是匹配的。
^
$
two word string