Peter Chen Peter Chen - 20 days ago 6
Python Question

using regular expression to split string in python

I use

re.compile(r"(.+?)\1+").findall('44442(2)2(2)44')


can get

['4','2(2)','4']


, but how can I get

['4444','2(2)2(2)','44']


by using regular expression?

Thanks

Answer

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+")
[match.group(0) for match in pattern.finditer('44442(2)2(2)44')]
Comments