Pockets via group method in Python search method
An alternative way to work with pockets is to use the group method. Its parameter specifies the number of the pocket to be output. If you specify 0 in the parameter or leave it empty, the entire substring matching the regular expression will be returned.
Let's say we have a line:
txt = '123 456 789'
Let's output the first match with the numbers specified by the regular expression of the search method. And then put the substring into three pockets, each of which we output using the group method:
res = re.search('(\d+)(\d+)(\d+)', txt)
print(res.group(0)) # '123'
print(res.group(1)) # '1'
print(res.group(2)) # '2'
print(res.group(3)) # '3'
Given a string:
txt = 'username:john'
Put 'username:' in the first pocket and 'john' in the second. Print all pockets to the console.
Given a string:
txt = '123 aaabbbccc'
Distribute all letter symbols into three pockets so that the substring consisting of the letter 'a' goes into the first pocket, 'b' goes into the second, 'c' goes into the third. Output all pockets to the console.