由特定字符组成的单词的正则表达式 [英] Regex for words formed with specific characters
本文介绍了由特定字符组成的单词的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一个正则表达式来匹配由特定字符组成的单词而不重复任何字符:例如,对于 a b c 和 d,如何指定一个正则表达式来匹配这些字符串:
I am looking for a regex to match words formed with specific characters without repeating any character: Example, for a b c and d, how to specify a regex to match those strings:
bdca(匹配)亚行(匹配)abcg(失败)aab(失败)我试过 ^[abcd]{1,4}$ 但它接受重复的字符(最后一个例子).
bdca (match) adb (match) abcg (fail) aab (fail) I tried with ^[abcd]{1,4}$ but it accepts repeated characters (last example).
请帮忙?
推荐答案
你可以使用这个基于负前瞻的正则表达式:
You can use this regex based on negative lookahead:
^(?:([abcd])(?!.*\1)){1,4}$
分手:
^ 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
这篇关于由特定字符组成的单词的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文