1

I need to generate a password regex with the following criteria:

A valid password must contain at least 8 characters. It must have at least one uppercase, one lowercase and one non alphabetic character.

So far, I created this pattern:

((?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,50})

But it still taking String that has no non alphabetic character. How can I do this? Thanks

Duc
  • 511
  • 1
  • 4
  • 18
  • 2
    "It must have at least one uppercase, one lowercase and one non alphabetic character." So if someone wants to brute force attack you've reduced the number of possible tries by a significant amount. Whoever started that practice should be spanked. Sorry I know it doesn't answer the question, unless you decide not to enforce that daft idea. – Popnoodles Feb 13 '13 at 17:45
  • How do you use that pattern? In which language? – Bergi Feb 13 '13 at 17:45
  • Without knowing which flavor you're working with, it'll be hard to help you much. [This other answer](http://stackoverflow.com/a/2370045/1147918) to a similar question may help you though. – Joe C Feb 13 '13 at 17:53
  • I am working with java but I think regex is st that not depends on the language you use though, correct me if I am wrong – Duc Feb 14 '13 at 10:38

1 Answers1

4

You can try this

                                           |->match 8 or more chars
                                         -----
^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[^a-zA-Z]).{8,}$
 ------------ ---------- ---------------
      |           |              |->matches further only if there is atleast 1 non alphabetic char
      |           |->matches further only if there is atleast 1 a-z
      |->matches further only if there is atleast 1 A-Z
Anirudha
  • 32,393
  • 7
  • 68
  • 89
  • @Joe yes definitely..thanks for your suggestion..but i wanted to match it as soon as possible...either way we get the right ans..but i guess the regex engine has to do less work with `.*?` although we can't say it optimized – Anirudha Feb 13 '13 at 18:16