[SOLVED] regular expression to split a string with comma outside parentheses with more than one level python

Issue

I have a string like this in python

filter="eq(Firstname,test),eq(Lastname,ltest),OR(eq(ContactID,12345),eq(ContactID,123456))"
    rx_comma = re.compile(r"(?:[^,(]|\([^)]*\))+")
    result = rx_comma.findall(filter)

Actual result is:

['eq(Firstname,test)', 'eq(Lastname,ltest)', 'OR(eq(ContactID,12345)', 'eq(ContactID,123456))']

Expected result is:

['eq(Firstname,test)', 'eq(Lastname,ltest)', 'OR(eq(ContactID,12345),eq(ContactID,123456))']

Any help is appreciated.

Solution

With PyPi regex module, you can use the code like

import regex
s = "eq(Firstname,test),eq(Lastname,ltest),OR(eq(ContactID,12345),eq(ContactID,123456))"
for x in regex.split(r"(\((?:[^()]++|(?1))*\))(*SKIP)(*F)|,", s):
    if x is not None:
        print( x )

Output:

eq(Firstname,test)
eq(Lastname,ltest)
OR(eq(ContactID,12345),eq(ContactID,123456))

See the Python and the regex demo.

Details:

  • (\((?:[^()]++|(?1))*\)) – Group 1 capturing a string between nested paired parentheses
  • (*SKIP)(*F) – the match is skipped and the next match is searched for from the failure position
  • | – or
  • , – a comma.

Answered By – Wiktor Stribi┼╝ew

Answer Checked By – Timothy Miller (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *