The easiest way is to use re.split
, putting the separator in a group will make it be kept in the output:
import re
string = "events.error > 5 AND timeseries.temperature < 20 AND variants.battery = 'Li' AND vehicle.name = 'Audi'"
re.split(r'(AND)', string)
Output:
['events.error > 5 ',
'AND',
' timeseries.temperature < 20 ',
'AND',
" variants.battery = 'Li' ",
'AND',
" vehicle.name = 'Audi'"]
Edit: as you included a second separator 'OR', the updated version:
import re
string = "events.error > 5 AND timeseries.temperature < 20 OR variants.battery = 'Li' AND vehicle.name = 'Audi'"
re.split(r'(AND|OR)', string)
Output:
['events.error > 5 ',
'AND',
' timeseries.temperature < 20 ',
'OR',
" variants.battery = 'Li' ",
'AND',
" vehicle.name = 'Audi'"]
If you want to get rid of the spaces around your strings, you can include any number of leading and trailing spaces in the separator, but outside of the group, so that they don't appear in the output:
import re
string = "events.error > 5 AND timeseries.temperature < 20 OR variants.battery = 'Li' AND vehicle.name = 'Audi'"
re.split(r'\s*(AND|OR)\s*', string) # include the spaces in the separator,
# but don't keep them
Output:
['events.error > 5',
'AND',
'timeseries.temperature < 20',
'OR',
"variants.battery = 'Li'",
'AND',
"vehicle.name = 'Audi'"]