Reputation: 147
I have tried to filter the content within the round brackets with regex.
s = "(time.timestamp > '2019-12-04 10:16:57+0100' AND car.Audi > 5) AND (time.timestamp > '2019-12-04 10:16:57+0100' AND car.VW > 5) OR (time.timestamp > '2019-12-04 10:16:57+0100' AND car.BMW > 5)"
What I would like to have as a return is a list of these contents and the ANDs
list = [
"(time.timestamp > '2019-12-04 10:16:57+0100' AND car.Audi > 5)",
"AND",
"(time.timestamp > '2019-12-04 10:16:57+0100' AND car.VW > 5)",
"OR",
"(time.timestamp > '2019-12-04 10:16:57+0100' AND car.BMW > 5)"
]
I tried re.split(r'\)\s*(AND|OR)\s*\(', s)
but some of the brackets were removed.
[
"(events.timestamp > '2019-12-04 10:16:57+0100' AND event_type.engine_error > 5",
'AND',
"events.timestamp > '2019-12-04 10:16:57+0100' AND event_type.turtle_lamp > 5)",
...
]
have you an idea how I can solve it best?
it doesn't have to be solved with Regex either.
Upvotes: 3
Views: 462
Reputation: 627103
The parentheses are removed because they are part of the consuming pattern. All consumed chars that are not captured are removed with re.split
.
Wrap the parentheses with lookarounds, a lookbehind at the start and a lookahead at the end:
re.split(r'(?<=\))\s*(AND|OR)\s*(?=\()', s)
^^^^^^^ ^^^^^^
Now, (?<=\))
matches a location that is immediately preceded with )
and (?=\()
matches a location immediately followed with (
.
Upvotes: 2