正则表达式匹配排列的字符集 [英] Regex to match permuted set of characters

查看:104
本文介绍了正则表达式匹配排列的字符集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想匹配以所有六个字符abcdef开头的字符串,而不考虑这六个字符在字符串开头出现的顺序.

I want to match strings which begin with all six characters abcdef, regardless of the order in which these six characters occur at the beginning of the string.

所有六个字符必须在字符串的前六个字符中出现一次,并且只能出现一次,以产生匹配.

All six characters must appear once, and once only, in the first six characters of the string, to produce a match.

例如"dfabce ..."是一个匹配项,但"aacdef ..."不是一个匹配项.

e.g. "dfabce..." is a match, but "aacdef..." is not.

正则表达式可以做到这一点,还是我需要解析器?

Can regular expressions do this, or do I need a parser?

推荐答案

当然,您可以使用肯定的先行断言来做到这一点:

Sure, you could do this with positive lookahead assertions:

^(?=.{0,5}a)(?=.{0,5}b)(?=.{0,5}c)(?=.{0,5}d)(?=.{0,5}e)(?=.{0,5}f).*

这将确保每个字母af出现在字符串的前6个字符中.

This will ensure that the letters a through f each appear in the first 6 characters of the string.

或具有否定的超前断言:

Or with a negative lookahead assertion:

^(?!(.).{0,4}\1|.(.).{0,3}\2|..(.).?.?\3|...(.).?\4|....(.)\5)[a-f]{6}

是的,我知道这看起来有些疯狂,但是基本上,它将确保前6个字符是af,并且不会重复第一个,第二个,第三个或第四个或第五个字符前6个字符以内.您需要有许多不同的替换方式,因为您不希望超前条件在前6个字符后流血"(即,您要匹配"dfabcee").

Yeah, I know this looks a little crazy, but basically, it will ensure that the first 6 characters are a through f and that the first, second, third, or fourth, or fifth character are not duplicated within the first 6 characters. You need to have so many different alternations because you don't want the lookahead condition to 'bleed' past the first 6 characters (i.e. you want to match "dfabcee").

或者,如果您选择的平台支持该功能,则可以使用后视式:

Or alternatively, if your chosen platform supports it, you could use a lookbehind:

^(([a-f])(?<!\2.*\2)){6}

这将确保前6个字符是af,并且确保它们不会出现在相同字符的实例之后.

This will ensure that the first 6 characters are a through f and that they do not appear after instance of the same character.

这篇关于正则表达式匹配排列的字符集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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