0

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?

1
  • For non-capturing groups you might read at here. Commented Aug 5, 2012 at 17:35

2 Answers 2

1

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']
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I think that's what I was looking for. I didn't know about capturing and non-capturing groups.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.