Reputation: 375
I have strings that I want to split of the form:
s = '|0-1|0.0|0.0+-0.0|0.8+-0.8|0.0+-0.0|-2.4+-2.4|5.9|'
I want to split it at every instance of |
and +-
, but NOT -
on its own. I'm trying to use re.split()
to do so because the regular split()
function doesn't take multiple delimiters, but I can't figure out the regular expressions syntax.
I tried the following:
splitted = re.split(r'\| | (+-)', s)
as it says that parentheses can be used to match the expression in the parentheses, but I get the error:
error: nothing to repeat
Upvotes: 0
Views: 344
Reputation: 20052
Just for fun, you could do that without regex
.
Here's how:
s = '|0-1|0.0|0.0+-0.0|0.8+-0.8|0.0+-0.0|-2.4+-2.4|5.9|'
print([j for i in s.split("|") for j in i.split("+-") if j])
Output:
['0-1', '0.0', '0.0', '0.0', '0.8', '0.8', '0.0', '0.0', '-2.4', '2.4', '5.9']
Upvotes: 1
Reputation: 66
You can do
import re
s = '|0-1|0.0|0.0+-0.0|0.8+-0.8|0.0+-0.0|-2.4+-2.4|5.9|'
re.split("\||\+\-", s)
The second |
should not contain any whitespace btw.
Also note that the returned list will contain empty strings because of s
starting and ending with |
.
Upvotes: 1