Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String with at least one uppercase, one lowercase, one special char, one digit and one white space [duplicate]

Tags:

regex

perl

I want a script to select a username. The rules for selecting a username are:

  • The minimum length of the username must be 5 characters and the maximum may be 10.
  • It should contain at least one letter from A-Z
  • It should contain at least one digit from 0-9
  • It should contain at least one character from amongst @#*=
  • It should not contain any spaces.

I have tried this:

if (( length $passwd[$i]<=10 && length $passwd[$i]>=5 && $passwd[$i] =~ /.*\p{Lu}/ && $passwd[$i] =~ tr/0-9//cd  && $passwd[$i] =~ /[a-z]/ ))
{   
    print "PASS\n";
}
else
{
    print "FAIL\n";
}
like image 326
Ricky Avatar asked Feb 01 '26 01:02

Ricky


1 Answers

As one perl regular expression, that could be:

if ($username =~ qr{^(?=.*[A-Z])(?=.*\d)(?=.*[@#*=])(?!.* ).{5,10}$}s) ...

Or:

if ($username =~ qr{^(?=.*[A-Z])(?=.*\d)(?=.*[@#*=])[^ ]{5,10}$}s)
like image 191
Stephane Chazelas Avatar answered Feb 03 '26 17:02

Stephane Chazelas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!