我正在尝试验证字符串以接受两个单词之间的空格(中间)。
我想要一个能够接受 "hello world "的regex。
我尝试使用 [\p{IsAlphabetic}\p{Digit}-_]{0,255}
.
先谢谢你
你需要使用 \\w+\\s+\\w+
如果你想匹配两个用空格分隔的字符串。
public class Main {
public static void main(String[] args) {
String[] testStrs = { "Hello", "Hello World", "123 456", "123456","Hello world good" };
for (String s : testStrs) {
System.out.println(s + " => " + s.matches("\\w+\\s+\\w+"));
}
}
}
输出。
Hello => false
Hello World => true
123 456 => true
123456 => false
Hello world good => false