This returns 'hhhhhhh' which is what I want
max(re.findall('h+', 'hhhhhhhahahahahahaaaaa'), key = len)
but this only returns a single 'ha'?
max(re.findall('(ha)+', 'hhhhhhhahahahahahaaaaa'), key = len)
How do I make it return 'hahahahaha'?
You'd need to put a non-capturing group around the part that you want to repeat. Then the outermost group should be used by default, as in the first example with h+
.
import re
res = max(re.findall('(?:ha)+', 'hhhhhhhahahahahahaaaaa'), key = len)
print(res)
Prints:
hahahahahaha
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.