Chux Uzoeto
Chux Uzoeto

Reputation: 1404

Regular Expression(Ruby): match non-word, but exclude spaces

^(?=(.*\d){4,})(?=(.*[A-Z]){3})(?!\s)(?=.*\W{2,})(?=(.*[a-z]){2,}).{12,14}$

The RegExp above is trying to:

But I am having a challenge getting this to avoid matching spaces. It seems like because \W also includes spaces, my preceding negative look-ahead on spaces is being ignored.

For example:

rubular link

Edited to provide further clarification:

Basically, I am trying to avoid having to spell out all the characters in the POSIX [:punct:] class ie !"#$%&'()*+,./:;<=>?@\^_\{|}~-` .. that is why I had a need to use \W .. But I would also want to exclude spaces

I can use a second pair of eyes, and more experienced suggestions here ..

Edited again, to correct mix-ups in counts specified in sub-patterns, as pointed out in the accepted answer below.

Upvotes: 1

Views: 1061

Answers (1)

Toto
Toto

Reputation: 91430

Instead of using dot ., use non spaces \S:

^(?=(.*\d){3,})(?=(.*[A-Z]){2})(?=.*\W{1,})(?=(.*[a-z]){1,})\S{12,14}$
//                                                  here ___^^

And is this a typo match at least 4 digits - (?=(.*\d){3,}), it should be:

match at least 3 digits - (?=(.*\d){3,})

or

match at least 4 digits - (?=(.*\d){4,})

Same for other counts.

Upvotes: 1

Related Questions