0

^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,15}$

My regex above works fine except it accepts space as input. How can i exclude spaces on my regex?

Any Perl related regex implementation will be fine. Thanks

user3483203
  • 50,081
  • 9
  • 65
  • 94
LogronJ
  • 561
  • 2
  • 4
  • 24
  • 3
    If you are using multiple `?=` in a regex something is wrong. Handle the logic in the code please. – user202729 Apr 04 '18 at 15:33
  • 4
    use `\S` instead of `.` – Pranav C Balan Apr 04 '18 at 15:33
  • 1
    Uh...? Java is related here? ... – user202729 Apr 04 '18 at 15:34
  • 1
    Cannot replicate in Java, but I might be assuming things. You probably want to edit your answer with a self-contained [MCVE](https://stackoverflow.com/help/mcve). – Mena Apr 04 '18 at 15:35
  • 1
    Should this be tagged PCRE instead of Perl? – DavidO Apr 04 '18 at 15:36
  • Change `^` to `^(?!.*?[ ])` – wolfrevokcats Apr 04 '18 at 15:38
  • @user202729, Re "*If you are using multiple ?= in a regex something is wrong.*", Nonsense. On the other hand, using multiple `.*?` is usually wrong, but not here. – ikegami Apr 04 '18 at 15:39
  • @ikegami `^(?=...$)(?=...$)` should be avoided anyway. I meant "multiple `?=` in the same place, at the begin of the regex, after a `^`, all terminates with `$`, but ... that's too verbose. – user202729 Apr 05 '18 at 01:20
  • @user202729, 1) huh? That didn't match what was the OP used. 2) Too verbose? LOL! You should see what the pattern looks like if `(?=)` wasn't used!!! True, I'd probably use multiple patterns and match operations, but that's not always possible, it's slower, and is not really any more readable than the addition of whitespace to the existing pattern – ikegami Apr 05 '18 at 02:11
  • @ikegami In pseudo-code: `if input matches /[A-Z]/ and input matches /[a-z]/ and input matches /[0-9]/ and input matches /[#?!@^*^$]/ and input matches /^.{8,15}$/`. More readable? – user202729 Apr 05 '18 at 02:13
  • @ikegami My point was, when it's possible it should be used, because it's (often) more readable. About "it's slower", it also depends on the implementation. – user202729 Apr 05 '18 at 02:14
  • This is completely different than what you originally said. – ikegami Apr 05 '18 at 02:18
  • You should read [Reference - Password Validation](https://stackoverflow.com/q/48345922/3600709) – ctwheels Jun 19 '19 at 14:17

1 Answers1

6

Change

.{8,15}

to

[^ ]{8,15}                       # No spaces.

or

\S{8,15}                         # No whitespace.

or

[A-Za-z0-9#?!@$%^&*\-]{8,15}     # Only allow specific characters.
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • My condition for this is: •Password must be case sensitive •Password must contain at least one special character •Password must contain at least one upper case character •Password must contain at least one numerical charcter •Password should not contain blank space •Password should consist of 8-15 characters – LogronJ Apr 05 '18 at 02:38
  • Your answer works if i put space in between but not if i put space at the beginning or end. @ikegami – LogronJ Apr 05 '18 at 02:43
  • It works if you use the change I posted. I have no idea why you're talking about putting spaces in various places – ikegami Apr 05 '18 at 02:47
  • i noticed that my error message in keycloak UI is not displayed if i change the user password with spaces(but there is no spaces of the pw in the background ).If the user will login the temporary password i gave, then he will update the password, right there if they input space they will be prompted an error. I think this is just a bug in the admin keycloak ui – LogronJ Apr 05 '18 at 04:11