This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center。
6年前关闭。
假设我有一个扫描仪,并且用户向该扫描仪添加了两个单词。
第一个单词前不应有空格。
两个单词之间只有一个空格。
第二个单词后面没有空格。
即
但
都是不正确的。
我尝试使用正则表达式,但没有成功。
编辑:只允许字母a到z(大写和小写)
在您弄清还允许使用大写字母后,我对其进行了编辑以使用posix字符类。
6年前关闭。
假设我有一个扫描仪,并且用户向该扫描仪添加了两个单词。
第一个单词前不应有空格。
两个单词之间只有一个空格。
第二个单词后面没有空格。
即
'hello world' is correct
但
'hello world bye'
' hello world'
'hello word '
'hello world'
都是不正确的。
我尝试使用正则表达式,但没有成功。
编辑:只允许字母a到z(大写和小写)
最佳答案
尝试
if (input.matches("\\p{Alpha}+ \\p{Alpha}+")) { // same as "[a-zA-Z]+ [a-zA-Z]+"
// good
} else {
// bad
}
在您弄清还允许使用大写字母后,我对其进行了编辑以使用posix字符类。