I want to build a Regex that does the following:
- Limit inputs to
0-9
,A-z
(upper and lower), and\-|/
- Make sure the first character is not 0.
So these should get through:
ASDFGHJKLQWERTYUIOPZXCVBNM
SUBSCRIBETOME
AHH-HHH01/\-65AHH
1234567890
1
||||||||||
But these shouldn't.
0
!
0123456789
!@#$%^&*()
{}:"
So far, I have this:
^[^0\S][\\\-/]*\w*\S*$
Per my understanding of Regex, this is what happens: The first [^] set blacklists empty spaces and 0 from being in the first character. The latter parts (so [\-/]* and \w* and \S*) sets matches for 0-9, A-z (upper and lower), and \ - | /.
My issue is that right now, the blacklist is taking up the first character, and there are other characters I would like to blacklist. Most special characters would get through too if it's in the first character. This will get through:
- !
- !AHHHHHHHH
As a result, I am looking to expand the blacklist, like so:
^[^0\S@#$%^&*()][\\\-/]*\w*\S*$
But doing it like this would require me to put in a lot of special characters, which I am trying to avoid.
Doing this, as suggested here: Regular expression for excluding special characters
^[\\\-/]*\w*\S*$
would change it from a blacklist to a whitelist system, but would get rid of my 2nd requirement, which is that "0" cannot be a starting character. This is the main factor which makes it hard for me to follow other answers on stackoverflow, as the first character has a slightly different limitation from the rest of the characters.
I am wondering whether there is an easier way of indicating the following logic:
"Regex needs to blacklist '0' and whitespaces from the 1st character, but also anything not part of the following whitelist"
Please let me know if more information is required.