[英]Regular Expression - Java not working
I have a line of Java code 我有一行Java代码
System.out.println("...Somtime".matches("^[^a-zA-Z]"));
Which returns false. 哪个返回false。 Why?
为什么? Can any one help?
有人可以帮忙吗?
String#matches
matches at both the ends, so your pattern should cover the complete string. String#matches
两端都匹配,因此您的模式应覆盖完整的字符串。 And also you don't need to give those anchors (Caret - ^)
at the beginning. 同样,您也不需要在一开始就使用锚点
(Caret - ^)
。 It is implicit. 它是隐式的。
Now, since your first three characters matches - [^a-zA-Z]
, while the later characters matches - [a-zA-Z]
. 现在,由于您的前三个字符匹配-
[^a-zA-Z]
,而后面的字符匹配- [a-zA-Z]
。
So, probably you want: - 因此,可能您想要:-
"...Somtime".matches("[^a-zA-Z]{3}[a-zA-Z]+")
String.matches("regex")
This method will match the regex against the WHOLE string. 此方法将使正则表达式与WHOLE字符串匹配。 If the string matches regex, it will return
true
and false
otherwise 如果字符串与regex匹配,则将返回
true
和false
否则
System.out.println("...Somtime".matches("^[^a-zA-Z]{3}[a-zA-Z]+"));
here for three dots you are using {3} and this return true
这是您正在使用{3}的三个点,此返回
true
System.out.println("Somtime".matches("^[^a-zA-Z]"));
it return false
它返回
false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.