-2

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

  • The minimum length of the username is 5 characters and the maximum is 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";
}
Borodin
  • 126,100
  • 9
  • 70
  • 144
Ricky
  • 51
  • 8
  • 2
    And what is your problem with what you have written? It doesn't look like a real attempt to write your own solution to me. Where is your attempt to check for the symbols? Why have you used `/.*\p{Lu}/` for the upper case letter, `/[a-z]/` for the lower case letter, and `tr/0-9//cd` for the digit? And why is your *username* called `$passwd[$i]`? Please make a reasonable attempt to do your own work and we will help you if you get stuck. – Borodin Dec 14 '13 at 11:05
  • Lot's of **regex** questions on the topic of [strong passwords](http://stackoverflow.com/search?tab=votes&q=[regex]%20strong%20password). – DavidRR Dec 14 '13 at 21:57
  • possible duplicate of [RegEx to make sure that the string contains at least one lower case char, upper case char, digit and symbol](http://stackoverflow.com/questions/1559751/regex-to-make-sure-that-the-string-contains-at-least-one-lower-case-char-upper) – amon Dec 16 '13 at 00:26

2 Answers2

1

I managed to get below Regex using lookarounds. I tried many examples to test it and seems like it is working as per your conditions. In case it does not work for any pattern, then let me know.

echo eff*sdE8ff | perl -n -e 'print if /^(?=.{5,10}$)(?=.*?[A-Z])(?=.*?\d)(?=.*[@#*=])(?!.*\s+)/'

UPDATE: After reading OP's comment...

Here is the code:

#!/usr/bin/perl 
use warnings;
use strict;

my @passwd = ('eff*sdE8ff', 'eff*sde8ff');

foreach (@passwd)
{
    if ($_ =~ /^(?=.{5,10}$)(?=.*?[A-Z])(?=.*?\d)(?=.*[@#*=])(?!.*\s+)/)
    {
        print "PASS: $_ \n";
    }
    else
    {
        print "FAIL \n";
    }
}

OUTPUT:

PASS: eff*sdE8ff 
FAIL 
slayedbylucifer
  • 22,878
  • 16
  • 94
  • 123
0

Here is a slighty more readable alternative for a novice user:

#!/usr/bin/perl
use strict;
use warnings;

my $name = $ARGV[0];

if((length($name)<5 || length($name)>10) ||
   ($name !~ /[A-Z]/)  ||
   ($name !~ /[0-9]/)  ||
   ($name !~ /[@#*=]/) ||
   ($name =~ / /)){
   print "FAIL\n";
} else {
   print "PASS\n";
}
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432