gpt4 book ai didi

正则表达式用于限制字符池中没有重复的字符

转载 作者:行者123 更新时间:2023-12-04 13:22:57 25 4
gpt4 key购买 nike

有没有一种方法可以编写正则表达式来匹配仅包含某些字符且从不重复这些字符的字符串?我已经使用一组代码编写了一些代码来实现此目的,但是我想知道是否有正则表达式可以做到这一点。

因此,例如,如果我只想要一个包含[A,B,C]的字符串,并且想要匹配一个从不重复任何字符的字符串,例如A,B,C,AB,AC,B,BC ,ABC等,但从不匹配AA,BB,CC等

谢谢!

最佳答案

使用negative lookahead assertion很容易做到:

^(?!.*(.).*\1)[ABC]+$

完全符合您的描述。

测试 live on regex101.com

说明:

^      # Start of the string
(?! # Assert that it's impossible to match...
.* # Any number of characters (including zero)
(.) # followed by one character (remember this one in group 1)
.* # that's followed by any number of characters
\1 # and the same character as before
) # End of lookahead
[ABC]+ # Match one or more characters from this list
$ # until the end of the string

关于正则表达式用于限制字符池中没有重复的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26470820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com