Problem Statement:
I’m trying to build a regular expression which accepts two consecutive special characters like: /_ or \\ or ./ or -- or \- or any other combination of special charcters (./\_-),in the regular expression mentioned below:
^[a-zA-Z0-9\d]{1}[a-zA-Z0-9\d._/\-]{0,49}$
What i’m doing wrong here?
mlorbetske’s regex can be rewritten a bit to remove the use of conditional regex. I also remove the redundant
0-9from the regex, since it has been covered by\d.The portion
(?:[a-zA-Z\d]|(?<![._/\\\-])[._/\\\-])matches alphanumeric character, OR special character.,_,/,\,-if the character preceding it is not a special character already. I also make the group non-capturing(?:pattern), since it seems that the regex is used for validation only.I made use of the zero-width negative look-behind assertion
(?<!pattern)to assert the character in front is not one of the special characters.