【问题标题】:How to split by commas that are not within parentheses?How to split by commas that are not within parentheses?
【发布时间】:2021-03-19 05:33:44
【问题描述】:

Say I have a string like this, where items are separated by commas but there may also be commas within items that have parenthesized content:

(EDIT: Sorry, forgot to mention that some items may not have parenthesized content)

"Water, Titanium Dioxide (CI 77897), Black 2 (CI 77266), Iron Oxides (CI 77491, 77492, 77499), Ultramarines (CI 77007)"

How can I split the string by only those commas that are NOT within parentheses? i.e:

["Water", "Titanium Dioxide (CI 77897)", "Black 2 (CI 77266)", "Iron Oxides (CI 77491, 77492, 77499)", "Ultramarines (CI 77007)"]

I think I'd have to use a regex, perhaps something like this:

([(]?)(.*?)([)]?)(,|$)

but I'm still trying to make it work.

【问题讨论】:

  • can you show what you have attempted so far?

标签: python regex


【解决方案1】:

Use a negative lookahead to match all the commas which are not inside the parenthesis. Splitting the input string according to the matched commas will give you the desired output.

,s*(?![^()]*))

DEMO

>>> import re
>>> s = "Water, Titanium Dioxide (CI 77897), Black 2 (CI 77266), Iron Oxides (CI 77491, 77492, 77499), Ultramarines (CI 77007)"
>>> re.split(r',s*(?![^()]*))', s)
['Water', 'Titanium Dioxide (CI 77897)', 'Black 2 (CI 77266)', 'Iron Oxides (CI 77491, 77492, 77499)', 'Ultramarines (CI 77007)']

【讨论】:

  • regex101.com strikes again! :) (I just commented here about it too an hour ago)
  • I have a similar problem but this doesn't work for me because there are inner parentheses. For example, "Water, Titanium Dioxide (CI 77897), Black 2 (CI 77266), Iron Oxides (CI 77491, 77492(w), 77499), Ultramarines (CI 77007)"
  • This doesn't work for matching parenthesis however, try this: s="b.buildPlanPHID,coalesce(concat('D', r.Id), concat('D',c.revisionID), concat('D', d.revisionID)) as revision_id ,d.Id as diff_id" which should break it into 3 tokens, but it creates more.
  • yep, this won't work on the string which contain parenthesis of level more than 1.
  • Was searching for a while and this is the only regex solution that worked for me
【解决方案2】:

You can just do it using str.replace and str.split. You may use any character to replace ),.

a = "Titanium Dioxide (CI 77897), Black 2 (CI 77266), Iron Oxides (CI 77491, 77492, 77499), Ultramarines (CI 77007)"
a = a.replace('),', ')//').split('//')
print a

output:-

['Titanium Dioxide (CI 77897)', ' Black 2 (CI 77266)', ' Iron Oxides (CI 77491, 77492, 77499)', ' Ultramarines (CI 77007)']

【讨论】:

  • Where is the string water ?
  • @AvinashRaj ohh! i just missed it in my string.
  • This solution does not split off items that do not end on a parenthesis (like Water in the example), so the string is incorrectly split.
【解决方案3】:

I believe I have a simpler regexp for this:

rx_comma = re.compile(r",(?![^(]*))")
result = rx_comma.split(string_to_split)

Explanation of the regexp:

  • Match , that:
  • IsNOTfollowed by:
    • A list of characters ending with ), where:
    • A list of characters between , and ) does not contain (

It will not work in case of nested parentheses, like a,b(c,d(e,f)). If one needs this, a possible solution is to go through a result of split and in case of strings having an open parentheses without closing, do a merge :), like:

"a"
"b(c" <- no closing, merge this 
"d(e" <- no closing, merge this
"f))

【讨论】:

    【解决方案4】:

    This version seems to work with nested parenthesis, brackets ([] or <>), and braces:

    def split_top(string, splitter, openers="([{<", closers = ")]}>", whitespace=" 
    	"):
        ''' Splits strings at occurance of 'splitter' but only if not enclosed by brackets.
            Removes all whitespace immediately after each splitter.
            This assumes brackets, braces, and parens are properly matched - may fail otherwise '''
    
    outlist = []
    outstring = []
    
    depth = 0
    
    for c in string:
        if c in openers:
            depth += 1
        elif c in closers:
            depth -= 1
    
            if depth < 0:
                raise SyntaxError()
    
        if not depth and c == splitter:
            outlist.append("".join(outstring))
            outstring = []
        else:
            if len(outstring):
                outstring.append(c)
            elif c not in whitespace:
                outstring.append(c)
    
    outlist.append("".join(outstring))
    
    return outlist
    

    Use it like this:

    s = "Water, Titanium Dioxide (CI 77897), Black 2 (CI 77266), Iron Oxides (CI 77491, 77492, 77499), Ultramarines (CI 77007)"
    
    split = split_top(s, ",") # splits on commas
    

    It's probably not the fastest thing ever, I know.

    【讨论】:

      【解决方案5】:

      Try the regex

      [^()]*([^()]*),?
      

      code:

      >>x="Titanium Dioxide (CI 77897), Black 2 (CI 77266), Iron Oxides (CI 77491, 77492, 77499), Ultramarines (CI 77007)"
      >> re.findall("[^()]*([^()]*),?",x)
      ['Titanium Dioxide (CI 77897),', ' Black 2 (CI 77266),', ' Iron Oxides (CI 77491, 77492, 77499),', ' Ultramarines (CI 77007)']
      

      see how the regex works http://regex101.com/r/pS9oV3/1

      【讨论】:

        【解决方案6】:

        Using regex, this can be done easily with the findall function.

        import re
        s = "Titanium Dioxide (CI 77897), Black 2 (CI 77266), Iron Oxides (CI 77491, 77492, 77499), Ultramarines (CI 77007)"
        re.findall(r"w.*?(.*?)", s) # returns what you want
        

        Use http://www.regexr.com/ if you want to understand regex better, and here is the link to the python documentation : https://docs.python.org/2/library/re.html

        EDIT : I modified the regex string to accept content without parenthesis : w[^,(]*(?:(.*?))?

        【讨论】:

          猜你喜欢
          • 2022-12-02
          • 2022-12-02
          • 2022-04-20
          • 2022-12-28
          • 2018-01-20
          • 2023-02-22
          • 2022-12-01
          • 2022-12-27
          • 2022-12-27
          相关资源
          最近更新 更多