Is there a way to write a regex to match a string that only contains certain characters, and never repeats those characters? I already wrote some code using a set to impleme
back referencing can be used. Here comes an example in PHP, which is compatible to Perl regular expressions:
$string = "A, B, C, AB, AC, B, BC, AABC";
if(preg_match('/([ABC])\1/', $string, $matches)) {
echo $matches[1] . " has been repeated\n";
} else {
echo "OK\n";
}
In the above pattern ([ABC]) is capturing group which can store one out of the characters A, B or C. \1 references this first capturing group, this makes the pattern matching if one those characters repeats.