如何使用正则表达式匹配任何字母组合? [英] How to match any combination of letters using regex?

查看:184
本文介绍了如何使用正则表达式匹配任何字母组合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能匹配字母a,b,c一次以任意组合和不同长度,如下所示:

表达式应该匹配这些情况:

b
$ b

  abc 
bc
a
b
bca

,但不应该匹配这些:

  abz 
aab
cc
x


解决方案

<使用正则表达式模式

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

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

示例

< img src =https://i.stack.imgur .com / gpgok.pngalt =

(以上输出来自 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)

这篇关于如何使用正则表达式匹配任何字母组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆