Regular expression that matches a string containing only certain letters
I've tried several combinations of regex to figure this out, but some or the condition doesn't work,
I have an input string that can only contain a specific set of specific characters
lets say A, B or C.
how can I match something like this?
ABBBCCC - isMatch True
AAASDFDCCC - isMatch false
ps. I am using C #
+2
source to share
2 answers
^[ABC]+$
Should suffice: using a character class or character set .
Anchors '^' and '$' will only be available to ensure that the entire string contains only those characters from start to end.
Regex.Match("ABACBA", "^[ABC]+$"); // => matches
Meaning: The character set does not guarantee the order in which the characters match.
Regex.Match("ABACBA", "^A+B+C+$"); // => false
Guaranteed order
+18
source to share