This program I am writing receives a string of differing size and with a differing number of brackets which within hold differing sizes of characters.
e.g: wysextplwqpvipxdv[srzvtwbfzqtspxnethm]syqbzgtboxxzpwr
I want to be able to slice this string into a list containing strings of all the parts that aren't in brackets.
e.g:
list[0] = wysextplwqpvipxdv
list[1] =syqbzgtboxxzpwr
I know of string.slice and I have read this: Explain Python's slice notation
Yet I am having trouble thinking of the way in which to put this into code.
The challenge being not knowing how many brackets and yet being able to cut the string into the list.
use re.split
on your brackets (non-greedy) regex:
import re
s = "wysextplwqpvipxdv[srzvtwbfzqtspxnethm]syqbzgtboxxzpwr"
toks = re.split("\[.*?\]",s)
print(toks)
result:
['wysextplwqpvipxdv', 'syqbzgtboxxzpwr']
warning: this doesn't work if brackets are nested. You'd have to use a more complex parser like pyparsing
in this case.
EDIT: in that case, nesting management possible with regex because we only consider the level outside the brackets. One of the new answers of regex to get all text outside of brackets does that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With