Q: Python regex match in groups I'm trying to put the following string into two groups as follows:
groups(1) = "Hello1 Hello2 Hello3 Hello4"
groups(2) = "Bye1 Bye2 Bye3 Bye4"

I tried to do this using the following code. However, boths groups do not contain all the desired words.
import re


string = "Hello1 Hello2 Hello3 Hello4 Bye1 Bye2 Bye3 Bye4"
pattern = r'(Hello[0-9])+\w(Bye[0-9])+'
result = re.search(pattern, string)
groups = result.group
print("Group 1: {}\nGroup 2: {}".format(groups(1), groups(2)))


A: This seems to be doing the job, although being different from the approach used in my initial post.
import re


string = "Hello1 Hello2 Hello3 Hello4 Bye1 Bye2 Bye3 Bye4"
patterns = [r'Hello[0-9]', r'Bye[0-9]']
results = [re.findall(patterns[idx], string) for idx in range(len(patterns))]
print("Results:", results)

