Reputation: 311
String that start AND ends with that character should not be a match.
Is it just easiest to do something like:
$string = '!test';
preg_match('/^(!?)([a-z0-9]{1,10})(!?)$/i', $string, $matches);
and examine $matches?
Upvotes: 0
Views: 257
Reputation: 54323
If you just have one character at the beginning or the end, it's as simple as saying this:
/^!|!$/
No need for a capture group.
Update: To make sure you get either an exclamation mark and ten letters or ten letters and an exclamation mark, build your pattern like this:
/^!?[a-z0-9]{1,10}$|^[a-z0-9]{1,10}!?$/
It looks like it's repeating itself, but it's actually way cheaper and faster than having three capture groups.
I put together a unit test in regex101 for this.
Upvotes: 3