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.
source share