正则表达式检查一组字符是否重复

编程入门 行业动态 更新时间:2024-10-27 16:32:43
本文介绍了正则表达式检查一组字符是否重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我有一组字符[ABC].我正在寻找一个正则表达式,该正则表达式将匹配超集的任何排列(空集除外),即

Suppose I have the set of characters [ABC]. I'm looking for a regex that would match any permutation of the superset except the empty set, i.e.

ABC ACB BAC BCA CAB CBA AB BC AC CB CA BA A B C

正则表达式应该(显然)不匹配空字符串.

The regex should (obviously) not match the empty string.

p.s.表达相同目标的另一种方法是将包含集合中每个字符的任何非空字符串最多匹配一次".

p.s. An alternative way to express the same objective is "match any non-empty string containing each character in the set at most once".

更新:集合[ABC]只是一个示例,实际集合也可能更大.带着这个问题,我希望找到一种通用"解决方案,而不是针对[ABC]的特定解决方案.

update: The set [ABC] is just an example, for the real set may also be bigger. With this question I was hoping to find a "general" solution rather than a particular one for [ABC].

推荐答案

感谢您的回答(尤其是anubhava和codaddict的回答),我能够找到此解决方案,我认为这很优雅,因为它只允许键入一次集:

Thanks to your answers (especially anubhava's and codaddict's) I was able to find this solution, that I think is pretty elegant because it allows to type the set only once:

\b(([ABC])(?!.*\2))+\b

需要

\b来匹配完整的单词;省略它们还会找到尊重所需属性的子词.要匹配完整的字符串,您显然可以这样做:

the \b are needed to match full words; omitting them will also find subwords respecting the required property. To match a full string, you'd obviously do:

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

更多推荐

正则表达式检查一组字符是否重复

本文发布于:2023-11-10 03:20:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574213.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符   正则表达式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!