Regex: optional special characters

I have a regex that has the following rules:

+ Must contain lower case letters
+ Must contain upper case letters
+ Must contain a number
+ Must contain a (defined)special character
+ At least 8 chars

This works, and my regex looks like this:

((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[~@#\$%\^&\*_\-\+=`|{}:;!\.\?\"()\[\]]).{8,25})

Now I want to change this regex only in one: Special characters should be available (and only those that I allow), but should be optional (no longer required) anywhere on the line. So the new rule will be

+ MAY contain (defined) special characters

What do I need to change for this?

Examples:

NicePassw0rd - NOT OK now, should be
NicePassw0rd%% - OK now (and still should be)
NicePassword - NOT OK now and shouldnt

You can check my regex: https://regex101.com/r/qN5dN0/1

+4
source share
1 answer

^ $ , . , ~@#$%^&*+=`|{}:;!.?\"()\[\]- ( , [A-Za-z0-9_] \w, PCRE /u):

^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[\w~@#$%^&*+=`|{}:;!.?\"()\[\]-]{8,25}$

- regex

:

  • ^ -
  • (?=.*\d) -
  • (?=.*[a-z]) - ASCII-
  • (?=.*[a-z]) - 1 ASCII
  • [\w~@#$%^&*+=`|{}:;!.?\"()\[\]-]{8,25} - (= ) 8-25 , -, _, ,
  • $ -

, , 0 , , . . , (?=\D*\d)(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z]):

^(?=\D*\d)(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])[\w~@#$%^&*+=`|{}:;!.?\"()\[\]-]{8,25}$
+6

Source: https://habr.com/ru/post/1617214/


All Articles