Peter Chen Peter Chen - 5 months ago 28
Python Question

using regular expression to split string in python

I use


can get


, but how can I get


by using regular expression?



No change to your pattern needed. Just need to use to right function for the job. re.findall will return a list of groups if there are capturing groups in the pattern. To get the entire match, use re.finditer instead, so that you can extract the full match from each actual match object.

pattern = re.compile(r"(.+?)\1+")
[ for match in pattern.finditer('44442(2)2(2)44')]