Python string to list?

Viewed 103

I'm trying to convert string to a list

 str = "ab(1234)bcta(45am)in23i(ab78lk)"

Expected Output

 res_str = ["ab","bcta","in23i"]

I tried removing brackets from str.

 re.sub(r'\([^)]*\)', '', str)
5 Answers

You may use a negated character class with a lookahead:

>>> s = "ab(1234)bcta(45am)in23i(ab78lk)"
>>> print (re.findall(r'[^()]+(?=\()', s))
['ab', 'bcta', 'in23i']

RegEx Details:

  • [^()]+: Match 1 of more of any character that is not ( and )
  • (?=\(): Lookahead to assert that there is a ( ahead

So many options here. One possibility would be using split:

import re
str = "ab(1234)bcta(45am)in23i(ab78lk)"
print(re.split(r'\(.*?\)', str)[:-1])

Returns:

['ab', 'bcta', 'in23i']

A second option would be to split by all paranthesis and slice your resulting array:

import re
str = "ab(1234)bcta(45am)in23i(ab78lk)"
print(re.split('[()]', str)[0:-1:2])

Where [0:-1:2] means to start at index 0, to stop at second to last index, and step two indices.

You may match all alphanumeric characters followed by a ( :

>>> re.findall('\w+(?=\()',str)
['ab', 'bcta', 'in23i']

or using re.sub as you were:

>>> re.sub('\([^)]+\)',' ',str).split()
['ab', 'bcta', 'in23i']

Use re.split

import re

str = "ab(1234)bcta(45am)in23i(ab78lk)"

print(re.split('\(.*?\)', str))

Returns:

['ab', 'bcta', 'in23i', '']

If you want to get rid of empty strings in your list, you may use a filter:

print(list(filter(None, re.split('\(.*?\)', str))))

Returns:

['ab', 'bcta', 'in23i']

Just for the sake of complexity :

>>>> str = "ab(1234)bcta(45am)in23i(ab78lk)"
>>>> res_str = [y[-1] for y in [ x.split(')') for x in str.split('(')]][0:-1]
['ab', 'bcta', 'in23i']
Related