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.
CodePudding user response:
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.
CodePudding user response:
Thank you Wiktor Stribiżew for providing the solution
Here is the solution:
import regex
filter = "eq(Firstname,test),eq(Lastname,ltest),OR(eq(ContactID,12345),eq(ContactID,123456))"
for s in regex.split(r"(\((?:[^()] |(?1))*\))(*SKIP)(*F)|,", filter):
if s is not None:
print( s )
Result:
eq(Firstname,test)
eq(Lastname,ltest)
OR(eq(ContactID,12345),eq(ContactID,123456))