rottentomato56
rottentomato56

Reputation: 1069

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. It will only return the entire string if the entire string is some integer multiple of 'aa'. Any ideas?

Upvotes: 0

Views: 263

Answers (2)

Wormbo
Wormbo

Reputation: 4992

Try something like e.g. (?:(?:expression){3})+ to find all multiples of three of the expression. 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 e.g. (?:(expression)\1{2})+ for multiples of three. Note that this may require backtracking if the expression is non-trivial and thus may be slow.

Upvotes: 0

BrenBarn
BrenBarn

Reputation: 251598

Just use (aa)+. (For findall, you'll want to use non capturing groups, so (?:aa)+.)

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

Upvotes: 1

Related Questions