开发者

Why doesn't ([A-Z]|[0-9])[\\.a-zA-Z0-9_-]{0,}$ match abc123 in Java?

I am trying to create a regular expression for the following rule.

RegEx: ([A-Z]|[0-9]开发者_开发技巧)[\\.a-zA-Z0-9_-]{0,}$

Rule: Combination of letters, hyphen and dash, also must contains upper case letter or number.

When I try the string abc123, it matches as expected on the JavaScript Regular Expression Tester.

But it failed in Java code:

str.matches("([A-Z]|[0-9])[\\.a-zA-Z0-9_-]{0,}$")

Any people know the reason? Is it something to do with my reg ex string?


The point is that you do not match the complete string but only part "123". Contrary String.matches only yields true if the complete string matches.

Note: Your regular expression is not similar to your description. It matches strings that start with upper case letter of number.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜