Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python slicing - everything but whats in brackets [duplicate]

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.

like image 293
Noob_Programmer Avatar asked Dec 18 '22 10:12

Noob_Programmer


1 Answers

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.

like image 115
Jean-François Fabre Avatar answered Mar 03 '23 11:03

Jean-François Fabre