I'd like to split the following string by the word 'and' except when the word 'and' is within quotes
string = "section_category_name = 'computer and equipment expense' and date >= 2015-01-01 and date <= 2015-03-31"
Desired Result
["section_category_name = 'computer and equipment expense'","date >= 2015-01-01","date <= 2015-03-31"]
I can't seem to find the correct regex pattern that splits the string correctly so that 'computer and equipment expense' is not split.
Here's what I tried:
re.split('and',string)
Result
[" section_category_name = 'computer "," equipment expense' ",' date >= 2015-01-01 ',' date <= 2015-03-31']
As you can see the result has split 'computer and equipment expense' into different items on the list.
I've also tried the following from this question:
r = re.compile('(?! )[^[]+?(?= *\[)'
'|'
'\[.+?\]')
r.findall(s)
Result:
[]
I've also tried the following from this question
result = re.split(r"and+(?=[^()]*(?:\(|$))", string)
Result:
[" section_category_name = 'computer ",
" equipment expense' ",
' date >= 2015-01-01 ',
' date <= 2015-03-31']
The challenge is that the prior questions on this topic do not address how to split a string by a word within quotes, since they address how to split a string by a special character or a space.
I was able to get the desired result if I modified the string to the following
string = " section_category_name = (computer and equipment expense) and date >= 2015-01-01 and date <= 2015-03-31"
result = re.split(r"and+(?=[^()]*(?:\(|$))", string)
Desired Result
[' section_category_name = (computer and equipment expense) ',
' date >= 2015-01-01 ',
' date <= 2015-03-31']
However I need the function to not split on 'and' within apostrophes instead of parenthesis