Regex Sucks!
But I needed to update a form field regex validation, so I tackled it head-on and won.
As a side note, regular expression creation is also one of the best examples of where Test Driven Development (TDD) works well. You really cannot create or maintain a regex without having all the test cases written first.
Validation Requirements
For a value entered in the field, it:
- Cannot start with a space.
- Cannot end with a space.
- May contain a one or more sapces (surrounded by other allowed characters).
- May also contain the following characters: A-Z, a-Z, 0-0, “-“, “_”, “\”, “/”.
- If a value is entered in the field the total character count may be from 1 to 20 characters.
Regex String
^([A-Za-z0-9\\\/_\-]|([A-Za-z0-9\\\/_\-][A-Za-z0-9\\\/_\- ]{0,18}[A-Za-z0-9\\\/_\-]))$
Regex Summary:
- For the first group (green):
- If the string contains only 1 character, ensure it is a valid character.
- OR, for the second set of 3 groups (purple):
- Ensure the first character is a valid character.
- Ensure 0-18 of the next characters is a valid character.
- Ensure the last character is a valid character.
- The combination of point 2.1, 2.2 & 2.3 handle scenarios of 2 up to 20 characters.
Test Data
kkdkd df sdf lk aj33<-- Pass 12345678901234567890<-- Pass kjlf l-Ka034-/\_ kj0<-- Pass adsfdsafsdaf dsfsdfa<-- Pass dlASjd83_-/\dfdsfsda<-- Pass adsfsdas sdafsdafsa<-- Pass: Contains multiple spaces in a row dsfsdf<-- Pass 2<-- Pass 11<-- Pass 111<-- Pass - dsf<-- Pass -df<-- Pass /dsaf<-- Pass \asdf<-- Pass _asdf<-- Pass _ sdfjlksdaflk jasdf<-- Pass 123456789012345678901<-- Fail. More than 20 characters asdf<-- Fail: Starts with a space adsfsdas sdafsdafs#<-- Fail: Ends with a # adsfsdafsdfsdfsadds<-- Fail: Starts with a space dfsdafljkasdfklsdsd <-- Fail: Ends with a space #<-- Fail: Invalid character only <-- Fail: Space only 2 <-- Fail: Ends with space 2<-- Fail: Starts with a space <-- Ignore: Blank line
See it in action
https://regex101.com/r/2uMivO/6
Tricky Bits
It took some trial and error to get this and using https://regex101.com was a huge help and time saver.
My final problem was allowing a single character, and that was whee the OR (“|”) came in, adding it to the start of the group.