Reputation: 53
I am looking for a regular expression to make sure that my filenames do not contain special characters and are limited to length 9.
I am using ^[a-zA-Z0-9][a-zA-Z0-9 ]{0,9}[a-zA-Z0-9]$
but it’s not able to match a single character like “a”.
Upvotes: 2
Views: 36
Reputation: 39444
The part after the first character can be made optional by surrounding it with parentheses ()
and then appending a question mark ?
. To not include this as a capturing group, use ?:
after the opening bracket:
^[a-zA-Z0-9](?:[a-zA-Z0-9 ]{0,9}[a-zA-Z0-9])?$
Upvotes: 2