3

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 5

5

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
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

1

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']

Comments

1

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']

Comments

0

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']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.