简体   繁体   中英

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

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))

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

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM