The input script receives lines, each of which is terminated by a pair of round brackets with arbitrary text between them. It is required to separate this text between the brackets from the rest of the text of the string, thus breaking the original string into two substrings.

For example, after processing the string

"Небольшая группа демонстрантов, протестующих у (какого-либо) учреждения (5 букв)" 

the output should be two substrings:

 "Небольшая группа демонстрантов, протестующих у (какого-либо) учреждения" 

and

 "(5 букв)" 

The brackets in general do not matter, but in the second substring, it would be desirable to get rid of them immediately.

The problem is that the parentheses in the text of the line can occur more than once. Help create a regular expression pattern. Thank you in advance.

    1 answer 1

     import re In [11]: s Out[11]: 'Небольшая группа демонстрантов, протестующих у (какого-либо) учреждения (5 букв)' In [12]: re.search(r'(.*)\s+\((.*)\)\s*$', s).groups() Out[12]: ('Небольшая группа демонстрантов, протестующих у (какого-либо) учреждения', '5 букв')