0

I have a string like below.

s = ({[test1, test2 ; test3 (New) ]})

Now I have a regex which will remove brackets and convert it into the list. Even if there are separated with a;b,c like. REGEX:

output = [i for i in re.split(r'\s*[(){}<>\[\],;\'"]\s*', s) if i]

But this regex is removing brackets from items of the list as well. ((New) in my case)

How to apply this regex for beginnig and end of the string. I know it can be done using ^ but not sure how?

Expected Output

['test1', 'test2', 'test3 (New)' ]

Output coming from above regex

['test1', 'test2', 'test3', 'New']

Any help?

Jayesh Dhandha
  • 1,983
  • 28
  • 50

2 Answers2

1
s = '({[test1, test2 ; test3 (New) ]})'

Based on your comment below I assume that the number of opening brackets of the whole string is equal to the number of closing brackets.

So removing the outer brackets first needs to know their number:

m = re.match('[({[]*', s)
n_brckt = m.span()[1] - m.span()[0]

Then remove the outer brackets ( - dependent on if there were found any...):

if n_brckt > 0:
    s = s[n_brckt:-n_brckt]
s = s.strip()

In: s
Out: 'test1, test2 ; test3 (New)'

Then you can split at all occurences of commas or colons optionally followed by a space:

In: re.split('[,;]+ *', s)
Out: ['test1', 'test2', 'test3 (New)']
SpghttCd
  • 10,510
  • 2
  • 20
  • 25
1

Using re.search

import re
s = "({[test1, test2 ; test3 (New) ]})"
m = re.search("\[(.*?)\]", s)
if m:
    #print(m.group(1).replace(";", ",").split(",")) 
    print([i.strip() for i in m.group(1).replace(";", ",").split(",")])

Output:

['test1', 'test2', 'test3 (New)']
Rakesh
  • 81,458
  • 17
  • 76
  • 113
  • Good Answer. Thanks! – Jayesh Dhandha May 22 '18 at 10:09
  • Check output for `(({{[[test1, test2 ; test3 (France)]]}}))` – Jayesh Dhandha May 22 '18 at 10:11
  • 1
    For a deep nested structure, regex will not work and can cause unwanted issues. It is best to find a parser according to your need. Ex: https://stackoverflow.com/questions/5454322/python-how-to-match-nested-parentheses-with-regex – Rakesh May 22 '18 at 10:13
  • Please note that `if m:` is not a valid test for success of `re.match`. You _could_ use `if m.end():` but however, clearly writing `if m.end() > 0:` or sth similar is always clearer to read and understand immediately. – SpghttCd May 22 '18 at 11:27