Regex help

I have this bit of regex. however, i cannot make it work such that it can accept the letter ‘a’ or ‘b’ or ‘c’, but not ‘aa’, ‘abc’, or any other characters other than letter ‘a’ or ‘b’ or ‘c’.


if (preg_match("~^(a)|(b)|(c){1}$~", $string)) { 
 
echo 'Valid'; } else { 
 
echo 'Invalid'; } 

I haven’t tested this, but my first guess would be

if (preg_match("~^[abc]{1}$~", $string)) { 
 
echo 'Valid'; } else { 
 
echo 'Invalid'; }  

Nope.

I want to allow either ‘a’ or ‘b’ or ‘c’ and it must be a single occurrence of a,b or c and nothing else.

My tests seem to work… check the code again, as I did have to fix a typo in my code.

this seems to work: “~^(a|b|c){1}$~”

The {1} is redundant there as (a|b|c) (without a “quantifier”) will match only one character. Note that (a|b|c) does the same, in terms of what it matches, as [abc].