Grouping Brackets in Python Regular Expressions
In the previous examples, the repetition operators only acted on the single character that came before them. What if you want them to act on multiple characters?
For this purpose, there are grouping brackets '(' and . They work like this: if something is in grouping brackets and immediately after ')'')' there is a repetition operator - it will affect everything that is inside the brackets.
In the following example, the search pattern is: letter 'x', then the string 'ab' one or more times, then the letter 'x':
txt = 'xabx xababx xaabbx'
res = re.sub('x(ab)+x', '!', txt)
print(res)
Result of code execution:
'! ! xaabbx'
Given a string:
txt = 'ab abab abab abababab abea'
Write a regular expression that will find strings with the pattern: string 'ab' repeated 1 or more times.