Do one test for each criteria, that will make it easier to maintain and to extend.
- First character is a capital letter:
^[[:upper:]]
- Last two characters are digits:
[[:digit:]][[:digit:]]$
- Length is 8 characters:
^.{8}$
- Other characters are lower-case (not explicitly stated):
^.[[:lower:]]*..$
With sed
(will only let through passwords matching the above criteria):
sed -r \
-e '/^[[:upper:]]/!d' \
-e '/[[:digit:]][[:digit:]]$/!d' \
-e '/^.{8}$/!d' \
-e '/^.[[:lower:]]*..$/!d'
This way you may easily add other criteria or modify the existing ones (more or less) independently of the others.
The -r
is to allow for the extended regular expression in criteria #3.
sed
thing that will comment on the ways that passwords fail the criteria:
sed -r \
-e '/^[[:upper:]]/!a\
No initial upper-case
' \
-e '/[[:digit:]][[:digit:]]$/!a\
No trailing digits
' \
-e '/^.{8}$/!a\
Not 8 characters
' \
-e '/^.[[:lower:]]*..$/!a\
Not only lower-case middle chars
You can't do that if you cram everything into a single regular expression...
(?=...)
construct there? Do you know what it does? As a Perl RE, that matches e.g. something like1xxxxxxX
. – ilkkachu Jan 09 '18 at 22:35I had absolutely no idea what the ((?-...) look ahead construct)) was until i researched it. I copied the above code from another post and altered it to try and make it fit my criteria.
– Matt Jan 11 '18 at 12:53