Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since you are dealing with nested parenthesis, regex can't help you much here. It cannot easily convert the string to the list, as you wanted at the end.</p> <p>You would better go with parsing the string yourself. You can try this code, just to meet your requirement at the end:</p> <h2>Parsing string into list without loosing parenthesis:</h2> <pre><code>def parse(s): li = [] open = 0 closed = False start_index = -1 for index, c in enumerate(s): if c == '(': if open == 0: start_index = index open += 1 elif c == ')': open -= 1 if open == 0: closed = True elif closed: li.append(s[start_index: index + 1]) closed = False elif open == 0 and c.isdigit(): li.append(c) return li </code></pre> <p>This will give you for the string <code>'1/(2/(3)2/4)s/5'</code> the following list:</p> <pre><code>['1', '(2/(3)2/4)s', '5'] </code></pre> <p>and for the string <code>'1/(15/-23)s/4'</code>, as per your changed requirement, this gives:</p> <pre><code>['1', '(15/-23)s', '4'] </code></pre> <p>Now, you need to take care of the breaking the parenthesis further up to get different list elements.</p> <hr /> <h2>Expanding the strings with parenthesis to individual list elements:</h2> <p>Here you can make use of a regex, by just dealing with inner-most parenthesis at once:</p> <pre><code>import re def expand(s): ''' Group 1 contains the string inside the parenthesis Group 2 contains the digit or character `s` after the closing parenthesis ''' match = re.search(r'\(([^()]*)\)(\d|s)', s) if match: group0 = match.group() group1 = match.group(1) group2 = match.group(2) if group2.isdigit(): # A digit after the closing parenthesis. Repeat the string inside s = s.replace(group0, ((group1 + '/') * int(group2))[:-1]) else: s = s.replace(group0, '/'.join(group1.split('/') + group1.split('/')[::-1])) if '(' in s: return expand(s) return s li = parse('1/(15/-23)2/4') for index, s in enumerate(li): if '(' in s: s = expand(s) li[index] = s.split('/') import itertools print list(itertools.chain(*li)) </code></pre> <p>This will give you the required result:</p> <pre><code>['1', '15', '-23', '-23', '15', '4'] </code></pre> <p>The above code iterates over the list generated from <code>parse(s)</code> method, and then for each element, recursively expands the inner most parenthesis.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload