[英]Java string.matches() returns wrong statement
I'm running some code through the eclipse debugger and a[1].matches("[a-zA-Z]")
is not equating to true
when a[1] = "ABCD"
( a
is a string array). 我正在通过eclipse调试器运行一些代码,当a[1] = "ABCD"
( a
是字符串数组)时, a[1].matches("[a-zA-Z]")
不等于true
。
I've read the javadoc on matches
and [a-zA-Z]
should be a valid regular expression.. 我已经阅读了matches
的javadoc, [a-zA-Z]
应该是一个有效的正则表达式..
Anyone know where I'm going wrong? 谁知道我哪里出错了?
Try using this expression: [a-zA-Z]*
(will match zero or more characters). 尝试使用此表达式: [a-zA-Z]*
(将匹配零个或多个字符)。
If you require at least one character, use: [a-zA-Z]+
如果您需要至少一个字符,请使用: [a-zA-Z]+
The expression you're using will only match a single alpha character since it's not quantified . 您使用的表达式只匹配单个字母字符,因为它没有量化 。
Try a[1].matches("[a-zA-Z]+")
. 尝试a[1].matches("[a-zA-Z]+")
。 It says "one or more characters" must match instead of only a single character. 它说“一个或多个字符”必须匹配,而不是只匹配一个字符。
Note that '*' instead of '+' matches "zero or more characters' so it will match empty String (probably not what you want). 请注意,'*'而不是'+'匹配“零个或多个字符”,因此它将匹配空字符串(可能不是您想要的)。
我认为它应该是a[1].matches("[a-zA-Z]*")
a[1].matches("[a-zA-Z\\s]+")
可能有帮助
[a-zA-Z]
would only accept a single letter. [a-zA-Z]
只接受一个字母。 You probably need [a-zA-Z]*
. 你可能需要[a-zA-Z]*
。
The reason that you're not matching that is string is that your RegEx expression is trying to match just a single character. 你不匹配字符串的原因是你的RegEx表达式试图只匹配一个字符。 Try this: 尝试这个:
a[1].matches("[a-zA-Z]*")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.