I am trying to create a regular expression to help me validate a particular string that users will be passing into my application. I thought I had it figured out until I found a few test cases that my expression gave a false positive for. Here are the details:
- The first six characters will be letters or spaces.
- The first character must be a letter.
- If the first six characters are not all letters, then the rest of the six are padded with spaces.
Here are some examples of valid strings:
Good:
ABCDEF0123
GHIJK 0123
LMNO 0123
PQR 0123
ST 0123
U 0123
So I quickly whipped up the below expression to validate these strings:
([a-zA-Z]){1}([a-zA-Z]|\\s){5}\\d{4}
The problem is this can create false positives because I am not properly meeting requirement number 3. Here are some strings that cause false positives:
Bad:
ABCD F0123
GHI JL0123
MN PQR0123
S UVWX0123
I’m not the best at regular expressions so I am kind of stumped as to how to proceed. How do I check the first six characters so that they are letters until a space is found and then it is padded until it reaches 6 characters. Thank you in advance for your help.