English 中文(简体)
例外的有效期限制未能达到单位标准
原标题:RegEx character length limit fails unit test
  • 时间:2012-01-15 18:30:47
  •  标签:
  • java
  • regex

我对创建复杂的“Ex”并不感兴趣。 我从另一个来源抄录了以下内容,以便在一定程度上强制执行一个附录的密码限制:

// 8 to 20 char, one digit, one letter
public static final String GOOD_PASSWORD_REGEX =
     "(^(?=.{8,20})(?=.*[a-zA-Z])(?=.*[\d]).*$)";

与此同时,这一单位测试失败了:

String tooLongPassword = "asdfghjkl123456789qwe";  // 21 characters
assertFalse(tooLongPassword.matches(ValidationContants.GOOD_PASSWORD_REGEX));

这是我针对这一“Ref”案,包括没有信件、没有数字的其他人以及所有其余通行证的少数类似测试案例之一。

错误在哪里?

最佳回答

In your version, the lookahead assertion only checks if it can match a string of 8-20 character length at the start of the string. This of course also succeeds in a string of length 21 and above.

So the $ needs to become part of the lookahead:

// 8 to 20 char, one digit, one letter
public static final String GOOD_PASSWORD_REGEX =
    "(^(?=.{8,20}$)(?=.*[a-zA-Z])(?=.*\d).*$)";

但是,为什么对密码规定最长期限? 另外,在<代码>d周围的<代码>[,不适用。

Furthermore, since you only need the regex to validate the password, not actually return it (because the way it s set up now it will return the entire input string), you can shorten the regex to:

"^(?=.{8,20}$)(?=.*[a-zA-Z])(?=.*\d)"
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签