Dave D.
Dave D.

Reputation: 767

How can I perform conditional splitting in python?

I want to split string with comma when comma is not surrounded by ().

str = r"a,b,c,test(1,2,3),g,h,test(2,4,6)"

Desired split

split = ['a','b','c','test(1,2,3)','g','h','test(2,4,6)']

how can I do using regex python

My efforts so far,

splits = [m.start() for m in re.finditer(',', string_i)]

paranths = {10: 16, 26: 32} #using function

lst = []

first = 1
for l in splits:
    print l
    for m in paranths:
        if (l < m and l < paranths[m]):
            #print m,':',l,':',paranths[m]
            lst.append(string_i[first:l])
            first = l+1
            break
            break

Upvotes: 3

Views: 6188

Answers (3)

Steven Senko
Steven Senko

Reputation: 127

As said above you can use negative look behind and ahead pattern matching.

import re

my_str = r"a,b,c,test(1,2,3),g,h,test(2,4,6)"

print(re.split('(?<!\(.),(?!.\))', my_str))

Upvotes: 4

Luchko
Luchko

Reputation: 1153

not regex but might work:

def my_split(s):

    splits0 = s.split(',')
    splits = []

    BEWEEN_PARENTHESIS = False

    # join everything between parenthesis
    for j, elem in enumerate(splits0):
        if not BEWEEN_PARENTHESIS:
            if "(" in elem:
                BEWEEN_PARENTHESIS = True
                start = j
            else:
                splits.append(elem)

        elif BEWEEN_PARENTHESIS and ")" in elem:

            BEWEEN_PARENTHESIS = False
            splits.append(",".join(splits0[start:j+1]))

    return splits

s = "a,b,c,test(1,2,3),g,h,test(2,4,6)"
print(my_split(s))

Upvotes: 2

ssc-hrep3
ssc-hrep3

Reputation: 16079

You can use a negative lookbehind and a negative lookahead to find all , which are not surrounded by a bracket )(.

(?<![)(]),(?![)(])

Here is a live example: https://regex101.com/r/uEyTN8/2


Details:

  • (?<! ) if the characters inside the parenthesis match before the next occurence, it will dismiss the match of the next occurrence
  • (?! ) if the characters inside the parenthesis match after the occurence, it will dismiss the match of this occurrence
  • [)(] match parenthesis

Upvotes: 3

Related Questions