简体   繁体   English

python正则表达式匹配表达式的倍数

[英]python regex matching a multiple of an expression

I know this is probably pretty basic, but I'm trying to create a regex expression that will only match a certain multiple of a group of characters. 我知道这可能很基本,但是我正在尝试创建一个仅与一组字符的某些倍数匹配的正则表达式。 For example, re.findall(expression, 'aaaa') will return 'aaaa' but re.findall(expression, 'aaa') will return 'aa', where expression is some regex that involves the pair aa. 例如,re.findall(expression,'aaaa')将返回'aaaa',但是re.findall(expression,'aaa')将返回'aa',其中expression是一些涉及对aa的正则表达式。 It will only return the entire string if the entire string is some integer multiple of 'aa'. 仅当整个字符串是“ aa”的某个整数倍时,它才会返回整个字符串。 Any ideas? 有任何想法吗?

Just use (aa)+ . 只需使用(aa)+ (For findall, you'll want to use non capturing groups, so (?:aa)+ .) (对于findall,您将要使用非捕获组,因此(?:aa)+ 。)

>>> re.findall('(?:aa)+', 'aa')
['aa']
>>> re.findall('(?:aa)+', 'aaaa')
['aaaa']
>>> re.findall('(?:aa)+', 'aaaaa')
['aaaa']

Try something like eg (?:(?:expression){3})+ to find all multiples of three of the expression. 尝试使用例如(?:(?:expression){3})+来查找表达式三个的所有倍数。 If the expression is shorter, you could also just write it as often as you want. 如果表达式较短,则也可以根据需要多次编写。

If you want to match exact duplications, try something like eg (?:(expression)\\1{2})+ for multiples of three. 如果要匹配精确的重复项,请尝试使用例如(?:(expression)\\1{2})+作为3的倍数。 Note that this may require backtracking if the expression is non-trivial and thus may be slow. 请注意,如果表达式不平凡,则可能需要回溯,因此速度可能很慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM