Python slicing - everything except characters in parentheses

This program, which I write, receives a string of different sizes and with a different number of brackets, which, within the retention range, differ in character sizes.
for example : wysextplwqpvipxdv [srzvtwbfzqtspxnethm] syqbzgtboxxzpwr
I want to be able to cut this line into a list containing lines of all parts that are not in brackets. , eg:

list[0] = wysextplwqpvipxdv list[1] =syqbzgtboxxzpwr 

I am aware of string.slice and I read the following: Explain Python fragment notation
However, I am having problems with how to do this in code.
The problem is not how many brackets and at the same time the ability to cut a line in the list.

+4
source share
1 answer

use re.split in brackets of (non-greedy) regular expressions:

 import re s = "wysextplwqpvipxdv[srzvtwbfzqtspxnethm]syqbzgtboxxzpwr" toks = re.split("\[.*?\]",s) print(toks) 

result:

 ['wysextplwqpvipxdv', 'syqbzgtboxxzpwr'] 

warning: this does not work if the brackets are nested. In this case, you will have to use a more complex parser, for example pyparsing .

EDIT: in this case, managing the attachment is possible with a regular expression, because we only consider the level outside the brackets. One of regex 's new answers for getting all the text outside the brackets does this.

+11
source

Source: https://habr.com/ru/post/1263046/


All Articles