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':

pythontxt = 'xx xax xaax xaaax'
res = re.sub('xa{1,2}x', '!', txt)

print(res)

Result of code execution:

python'xx ! ! xaaax'

Example

In this example, the search pattern looks like this: letter 'x', letter 'a' two or more times, letter 'x':

pythontxt = 'xx xax xaax xaaax'
res = re.sub('xa{2,}x', '!', txt)

print(res)

Result of code execution:

python'xx xax ! !'

Example

In this example, the search pattern looks like this: letter 'x', letter 'a' three times, letter 'x':

pythontxt = 'xx xax xaax xaaax'
res = re.sub('xa{3}x', '!', txt)

print(res)

Result of code execution:

python'xx xax xaax !'

Example

In this example, the search pattern looks like this: letter 'a' ten times:

pythontxt = 'aaa aaaaaaaaaa aaa'
res = re.sub('a{10}', '!', txt)

print(res)

Result of code execution:

python'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:

pythontxt = 'xx xax xaax xaaax'
res = re.sub('xa{,3}x', '!', txt)

print(res)

Result of code execution:

python'! ! ! !'

Example

A zero before 3 is also acceptable:

pythontxt = 'xx xax xaax xaaax'
res = re.sub('xa{0,3}x', '!', txt)

print(res)

Result of code execution:

python'! ! ! !'

Practical tasks

Задание 1

Given a string:

pythontxt = 'aa aba abba abbba abbbba abbbbba'

Write a regular expression that will find the strings 'abba', 'abbba', 'abbbba' and only them.

Задание 2

Given a string:

pythontxt = '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).

Задание 3

Given a string:

pythontxt = '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).