如何编写与任何顺序的字符匹配的正则表达式?

Mut*_*tor 4 ruby regex

我正在尝试编写一个正则表达式,它将匹配一组字符而不考虑顺序.例如:

str = "act" 
str.scan(/Insert expression here/)
Run Code Online (Sandbox Code Playgroud)

会匹配:

cat
act
tca
atc
tac
cta
Run Code Online (Sandbox Code Playgroud)

但是不匹配ca,ac或者cata.

我在StackOverflow上阅读了很多类似的问题和答案,但没有找到一个与我的目标完全匹配的问题.

为了澄清一点,我使用ruby并且不想允许重复字符.

ste*_*ema 5

这是你的解决方案

^(?:([act])(?!.*\1)){3}$
Run Code Online (Sandbox Code Playgroud)

在Regexr上看到它

^                  # matches the start of the string
    (?:            # open a non capturing group 
        ([act])    # The characters that are allowed and a capturing group
        (?!.*\1)   # That character is matched only if it does not occur once more, Lookahead assertion
    ){3}           # Defines the amount of characters
$
Run Code Online (Sandbox Code Playgroud)

唯一的特殊想法是前瞻性断言,以确保角色不会重复.

^并且$是锚点以匹配字符串的开头和结尾.