1

Please give me the code in regex for password validation in java which should consist of one Caps character,one integer ,one following symbols( @,#,$,%,^,&,+,=) and small characters.

I have been trying this with different separate regular expressions and one combined regular expression.

Actually i am already having a single regex that evaluates all the conditions in javascript. I am not able to use it in Java back end. I tried by escaping \. Its also not working.

Here is my code:

    Pattern pattern = Pattern.compile("/.*(?=.{6,})(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$/");
    Matcher matcher = pattern.matcher("Aa@1");
    if(matcher.matches()){
        System.out.println("Matched");
    }
    else{
        System.out.println("No mat");
    }

The original javascript regex is

/.*(?=.{6,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$/

In that the \d gave me error due to the escaping character. So, i added another \ before that in the Java Version.

I am not able to understand what is going wrong.

Thanks in Advance.

gauti
  • 1,264
  • 3
  • 17
  • 43
  • You should give examples of things that you tried. – gpojd Dec 27 '11 at 15:04
  • 4
    @anubhava, can be easily done with lookaheads if supported. And even without lookarounds you could just permutate the possible orders, altho it's not practical in this case, it's possible. – Qtax Dec 27 '11 at 15:32
  • @Qtax: Yes, with permutation of all the orders (and there would be many) it is possible but not sure how it can be **easily** done using lookaheads by avoiding these permutations. – anubhava Dec 27 '11 at 15:40
  • Do you want to forbid special chars other than the 7 listed? (please don't) Otherwise why not just say "there must be one non-alpha, non-numeric" char. – Thomas Ahle Dec 27 '11 at 16:26
  • @anubhava - You would do well to go and read [Mastering Regular Expressions (3rd Edition)](http://www.amazon.com/Mastering-Regular-Expressions-Jeffrey-Friedl/dp/0596528124 "By Jeffrey Friedl. Best book on Regex - ever!"). There are _LOTs_ of things that regex can do (quickly and reliably) that many here profess to be impossible. You see, regex engines have long since advanced _way_ beyond _REGULAR_ (in the theoretical sense) regular expressions! The problem posed here is easily solved with any modern regex engine which features lookahead. See Tim's answer. – ridgerunner Dec 27 '11 at 16:31

2 Answers2

4

You were nearly there, but you missed a few details:

First, the starting point is bad - that JavaScript regex is ugly. Instead of

/.*(?=.{6,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$/

use this:

/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).{6,}$/

Then, to translate the regex to Java, you need to remove the delimiters (and use quotes instead, not additionally like you did) and double the backslashes (like you already did):

Pattern pattern = Pattern.compile("^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).{6,}$");

Now it should work.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
1

You don't need the /'s in Java. It will actually match a slash. Also, the leading .* is useless (although it won't affect the result).

Pattern pattern = Pattern.compile("(?=.{6,})(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$");
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • It might affect the processing time, though, especially if the validation fails. – Tim Pietzcker Dec 27 '11 at 16:16
  • I don't think you need the final $. Also you could substitute one of your look aheads for the final. * – Thomas Ahle Dec 27 '11 at 16:22
  • I tried both expression using compilation time.This one("(?=.{6,})(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$") takes less time compare to tim's exp. – gauti Dec 29 '11 at 04:57