Regex nested parenthesis in python

前端 未结 4 1967
灰色年华
灰色年华 2021-01-03 03:41

I have something like this:

Othername California (2000) (T) (S) (ok) {state (#2.1)}

Is there a regex code to obtain:

Other         


        
4条回答
  •  臣服心动
    2021-01-03 04:00

    Despite what I have said in the comments. I've found a way around:

    (?(?=\([^()\w]*[\w.]+[^()\w]*\))\([^()\w]*([\w.]+)[^()\w]*\)|.)(?=[^{]*\})|(?

    Explanation:

    (?                                  # If
    (?=\([^()\w]*[\w.]+[^()\w]*\))      # There is (anything except [()\w] zero or more times, followed by [\w.] one or more times, followed by anything except [()\w] zero or more times)
    \([^()\w]*([\w.]+)[^()\w]*\)        # Then match it, and put [\w.] in a group
    |                                   # else
    .                                   # advance with one character
    )                                   # End if
    (?=[^{]*\})                         # Look ahead if there is anything except { zero or more times followed by }
    
    |                                   # Or
    (?

    Online demo

提交回复
热议问题