Mahdi
Mahdi

Reputation: 3349

Regular expression for a string containing at least one number

Can someone please explain what the following regular expression means?

^(?=.*[\p{L}\p{M}0-9]).{6,50}$

It forces users to have at least one number in their username.

How should I modify it to remove this constraint?

Upvotes: 2

Views: 131

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626748

You need to remove the 0-9 constraint set in the look-ahead:

^(?=.*[\p{L}\p{M}]).{6,50}$

Now, it allows a string containing any symbols but a newline, from 6 to 50 occurrences, and at least one Unicode letter.

To use it in Java, you need to double-escape backslashes:

String pattern = "^(?=.*[\\p{L}\\p{M}]).{6,50}$";

Upvotes: 1

Related Questions