且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用正则表达式匹配任何字母组合?

更新时间:2023-02-25 22:29:29



  \b(?!(?:。\B)*(。)(?: \B。)* \ 1)[abc] + \ b 

您可以使用此与任何设置和大小的模式,只需用所需的设置替换 [abc] ...
$ b $ hr

示例


(以上输出来自 myregextester


How can I match letters a,b,c once in any combination and varying length like this:

The expression should match these cases:

abc
bc
a
b
bca

but should not match these ones:

abz
aab
cc
x

Use regex pattern

\b(?!(?:.\B)*(.)(?:\B.)*\1)[abc]+\b

You can use this pattern with any set and size, just replace [abc] with desired set...


Example:

(above output is from myregextester)