我正在寻找一个正则表达式匹配与特定字符形成的单词而不重复任何字符:例如,对于abc和d,如何指定正则表达式匹配这些字符串:
bdca(匹配)adb(匹配)abcg(失败)aab(失败)我尝试使用^ [abcd] {1,4} $但它接受重复的字符(最后一个例子).
请帮忙吗?
您可以使用此正则表达式基于负前瞻:
^(?:([abcd])(?!.*\1)){1,4}$
Run Code Online (Sandbox Code Playgroud)
分手:
^ 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
114 次 |
| 最近记录: |