【发布时间】:2018-01-09 15:52:48
【问题描述】:
我想检查用户输入的字符串(如'hello')是否只包含一个单词而没有其他内容。就像只对那些只包含 [a-zA-Z] 并且没有空格、点、下划线或其他单词的情况一样。
例如:
'hello' true
'hello_' false
'hello world' false
'h.e.l.l.o' false
我不知道如何编写正则表达式。需要帮助。
【问题讨论】:
-
您在问题中写了它,只需添加
+即可:[a-zA-Z]+。您可能还想锚定它:^[a-zA-Z]+$. -
不需要正则表达式。
str.isalpha可以。 -
hllo是一个单词true还是false? -
@usr2564301 是真的。我想要一个带有 [a-zA-Z] 的字符串。