我正在寻找一个正则表达式来匹配由特定字符组成的单词而不重复任何字符:例如,对于 a b c 和 d,如何指定一个正则表达式来匹配这些字符串:
bdca(匹配)
亚行(匹配)
abcg(失败)
aab(失败)
我试过 ^[abcd]{1,4}$ 但它接受重复的字符(最后一个例子)。
请任何帮助?
最佳答案
您可以基于负前瞻使用此正则表达式:
^(?:([abcd])(?!.*\1)){1,4}$
RegEx Demo
分手:
^ Line start
(?: Start non-capturing group
([abcd]) Match a or b or c or d and group it
(?!.*\1) Negative lookahead to fail the match if same grouped char is ahead
){1,4} 1 to 4 occurrences of non-capturing group
$ Line end
关于由特定字符组成的单词的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34046756/