2

I used Regex to verify a password with a minimum of 8 characters containing 1 uppercase, 1 lowercase, 1 special character, and 1 numeric value. The problem I encountered is that if people from Russia or any other country whose language is different from English try to enter the password using the default keyboard, it will create a problem for them.

Currently i have set this regex condition for my application :

String regex = "^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$";

It works if the user sets the password in English. But it does not work if the user's language is different. (A password like Испытание@123 will fail). I searched on google about russian regex for the default keyboard and found a solution for the russian keyboard.

Do i need to work out for all different language for password validation regex?

Is there an alternative solution so that I can check the password check for any default keyboard layout with the password validation that I want?

Rabindra Acharya
  • 1,868
  • 2
  • 18
  • 32

1 Answers1

2

You can use \p{Ll} to match any Unicode lowercase letters and \p{Lu} to match any Unicode uppercase letters:

String regex = "^(?=\\P{Lu}*\\p{Lu})(?=\\P{Ll}*\\p{Ll})(?=[^0-9]*[0-9])(?=[^#?!@$%^&*-]*[#?!@$%^&*-]).{8,}$";

Note the .*? in the lookaheads is not efficient, I used the reverse patterns (\P{Lu} matches any char that is not a Unicode uppercase letter, \P{Ll} matches any char other than a Unicode lowercase letter, etc.) in line with the principle of contrast.

If you need to support any Unicode digits, too, replace (?=[^0-9]*[0-9]) with (?=\\P{Nd}*\\p{Nd}) where \P{Nd} matches any char other than a Unicode digit and \p{Nd} matches any char other than a Unicode decimal digit.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • It doesn't work for python as it gives issue bad escape \P – Himal Acharya Sep 20 '22 at 08:21
  • @HimalAcharya Of course, it is a Java solution, the question is tagged with Java and Android tags. In Python, you need to install the [PyPi regex](https://pypi.org/project/regex/) library for these Unicode property classes to work. See the [Python regex matching Unicode properties](https://stackoverflow.com/q/1832893/3832970) post. Also, see [this post of mine](https://stackoverflow.com/a/36188204/3832970) showing how to use `re` to match any uppercase letters. – Wiktor Stribiżew Sep 20 '22 at 08:31