Find all characters, that are not in brackets | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 8

Find all characters, that are not in brackets

"hallo##abc[def]fg[hij]##[def]bc" I want to find hallo, abc,fg and bc but only find fg. import re field = "hallo##abc[def]fg[hij]##[def]bc" x = re.findall("[(?<=\])^][a-z\#]*[(?=\[)(?=$)]", field)

22nd Jul 2020, 9:50 AM
Oma Falk
Oma Falk - avatar
5 Answers
+ 7
♤♢☞ 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 ☜♢♤ , What do you mean exactly when you say that this is a dangerous solution?
23rd Jul 2020, 9:19 AM
Lothar
Lothar - avatar
+ 6
For all those who are not familiar with RegEx, (e.g. me 😅) here another solution: inp = "hallo##abc[def]fg[hij]##[def]bc" inp = inp.replace('##',',') inp = list(inp) while '[' in inp: inp[inp.index('['):inp.index(']')+1] = '' print(''.join([i for i in inp])) It can be modified to create a single string, not words.
22nd Jul 2020, 3:49 PM
Lothar
Lothar - avatar
+ 4
Lothar I'm also not familiar that even I didn't knew that the 're' module is called regex😅 before this thread.
22nd Jul 2020, 3:59 PM
Arctic Fox
Arctic Fox - avatar
+ 1
x = re.findall("(.*?)(?:\[.*?\]|$)",field)
22nd Jul 2020, 10:13 AM
Bahhaⵣ
Bahhaⵣ - avatar
22nd Jul 2020, 11:30 AM
Oma Falk
Oma Falk - avatar