Peter Chen Peter Chen - 1 year ago 64
Python Question

using regular expression to split string in python

I use


can get


, but how can I get


by using regular expression?


Answer Source

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')]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download