How to split a string by commas positioned outside of parenthesis?

前端 未结 10 2082
南方客
南方客 2020-11-27 19:36

I got a string of such format:

\"Wilbur Smith (Billy, son of John), Eddie Murphy (John), Elvis Presley, Jane Doe (Jane Doe)\"

so basicly i

10条回答
  •  难免孤独
    2020-11-27 20:19

    An attempt on human-readable regex:

    import re
    
    regex = re.compile(r"""
        # name starts and ends on word boundary
        # no '(' or commas in the name
        (?P\b[^(,]+\b)
        \s*
        # everything inside parentheses is a role
        (?:\(
          (?P[^)]+)
        \))? # role is optional
        """, re.VERBOSE)
    
    s = ("Wilbur Smith (Billy, son of John), Eddie Murphy (John), Elvis Presley,"
         "Jane Doe (Jane Doe)")
    print re.findall(regex, s)
    

    Output:

    [('Wilbur Smith', 'Billy, son of John'), ('Eddie Murphy', 'John'), 
     ('Elvis Presley', ''), ('Jane Doe', 'Jane Doe')]
    

提交回复
热议问题