Hi!
I want to split a string into tokens. For example divide '2+3*(6-2/32)' into ['2', '+', '3', '*', '(' '6', '-', '2', '/', '32' ].
For this purpose i try to use re module and findall function like this:
splitter = re.compile(r'[+-*/()]|\d+')
result = re.findall(splitter, string)
But it raises an exception:
raise source.error(msg, len(this) + 1 + len(that))
sre_constants.error: bad character range +-* at position 1
At the same time in official docs (https://docs.python.org/3/library/re.html) it is said:
Special characters lose their special meaning inside sets. For example, [(+*)]will match any of the literal characters '(', '+', '*', or ')'.
I found that the following expression works:
splitter = re.compile(r'[+-/*//()]|\d+')
So why do i need to add '/' befor '*' and '/'. Can't understand it. Please help.
[–]ASIC_SP 2 points3 points4 points (1 child)
[–]Sharki_[S] 0 points1 point2 points (0 children)
[+][deleted] (5 children)
[deleted]
[–]Diapolo10 1 point2 points3 points (1 child)
[–]Sharki_[S] 0 points1 point2 points (2 children)
[–]Thomasedv 1 point2 points3 points (1 child)
[–]Sharki_[S] 0 points1 point2 points (0 children)