开发者

Why am I getting illegal escape characters with regexp in Java? [duplicate]

This question already has answers here: Why does this Java regex cause "illegal escape character" errors? (7 answers) Closed 3 years ago.

I'm working on a simply password strength checker and i can not success applying regular expressions. In different resources different kinds of regular expressions are defined. i also find javascript regular expressions but had some problems to adapt.

First, these are the regular expressions i need for JAVA:

  • at least one lower case letter
  • at least one upper case letter at least
  • one number at least three numb开发者_开发知识库ers at
  • least one special character at least
  • two special characters both letters
  • and numbers both upper and lower case
  • letters, numbers, and special characters

My goal is not being a regular expression expert i just want to use the parts i need.If you direct me good tutorials which discusses above for java, will be appreciated.

Second, in this link some of them are mentioned but i'm getting problems to apply them.

here does not catch the lower case letter (my pass: A5677a)

if (passwd.matches("(?=.*[a-z])")) // [verified] at least one lower case letter {Score = Score+5;}

Also here i'm getting error : illegal escape character.

if (passwd.matches("(?=.*\d)")) // [verified] at least one number

Finally, are these expressions all different in different kind of programming or script languages?


The issue with the first regex ("(?=.*[a-z])") is that you are trying to match a sequence that ends with a-z; the * applies to the ., which overall says "match anything, then end with a letter from a to z."

To pass a literal backslash, you need to escape it as such: "(?=.*\\d)".

The illegal escape sequence is because Java tries to process the escape sequences in the string before it gets passed to the regex - \d does not map to anything, as opposed to \t or \n which map to tab and newline.

To help streamline your development process with regular expressions, I would suggest using a tool such as The Regex Coach that will allow you to test your regexes before you compile the program, so that you can see the results in real time. There are also online resources such as RegExr.

Based on the problems you're having, it wouldn't hurt to try running through some regular expression tutorials, either.


Judging by the amount of regular expressions you need, maybe it's time for you to become a regular expression expert. Check out the tutorial on regular-expressions.info, that's what taught me regexp.


I think that using a regular expression in this case would be pretty inefficient. A regular expression is not going to keep track of how many xs, ys, and zs you have (where x, y, and z are whatever criteria you list above) - this means you will need to apply multiple regular expressions (as you are doing in your example), each of which can take up to linear time.

Why not just write a processing function that examines every character in your password instead? You can do your entire check in a single pass.

Partial example:

public boolean isStrong(String toCheck) {
    boolean hasLower = false;
    boolean hasUpper = true;

    for (int i = 0; i < toCheck.length(); i++) {
      char next = toCheck.charAt(i);
      if (Character.isLower(next)) {
        hasLower = true;
      } else if (Character.isUpper(next)) {
        hasUpper = true;
      }
    }

    return hasLower && hasUpper;
}

Edit: I just noticed you are calculating a score rather than checking weak vs. strong; this can easily be adapted accordingly.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜