Curly Braces in Python Regular Expressions
The operators '+', '*', '?' are good, but they do not allow you to specify a specific number of repetitions. In this case, the operator {} will come to your aid.
It works like this: {5} - five repetitions, {2.5} – repeated two to five times (both inclusive), {2,} - repeated two or more times. See examples:
Example
In this example, the search pattern looks like this: letter 'x', letter 'a' once or twice, letter 'x':
txt = 'xx xax xaax xaaax'
res = re.sub('xa{1,2}x', '!', txt)
print(res)
Result of code execution:
'xx ! ! xaaax'
Example
In this example, the search pattern looks like this: letter 'x', letter 'a' two or more times, letter 'x':
txt = 'xx xax xaax xaaax'
res = re.sub('xa{2,}x', '!', txt)
print(res)
Result of code execution:
'xx xax ! !'
Example
In this example, the search pattern looks like this: letter 'x', letter 'a' three times, letter 'x':
txt = 'xx xax xaax xaaax'
res = re.sub('xa{3}x', '!', txt)
print(res)
Result of code execution:
'xx xax xaax !'
Example
In this example, the search pattern looks like this: letter 'a' ten times:
txt = 'aaa aaaaaaaaaa aaa'
res = re.sub('a{10}', '!', txt)
print(res)
Result of code execution:
'aaa ! aaa'
Example
In this example there is such a template: letter 'x', letter 'a' three times times or less, letter 'x'. To implement it, you don’t have to specify a number before the figure 3, but just put a comma:
txt = 'xx xax xaax xaaax'
res = re.sub('xa{,3}x', '!', txt)
print(res)
Result of code execution:
'! ! ! !'
Example
A zero before 3 is also acceptable:
txt = 'xx xax xaax xaaax'
res = re.sub('xa{0,3}x', '!', txt)
print(res)
Result of code execution:
'! ! ! !'
Practical tasks
Given a string:
txt = 'aa aba abba abbba abbbba abbbbba'
Write a regular expression that will find the strings 'abba', 'abbba', 'abbbba' and only them.
Given a string:
txt = 'aa aba abba abbba abbbba abbbbba'
Write a regular expression that will find strings of the form 'aba' in which 'b' occurs less than 3-th times (inclusive).
Given a string:
txt = 'aa aba abba abbba abbbba abbbbba'
Write a regular expression that will find strings of the form 'aba' in which 'b' occurs more than 4-th times (inclusive).