python - re.split on multiple characters (and maintaining the characters) produces a list containing also empty strings -
i need split mathematical expression based on delimiters. delimiters (
, )
, +
, -
, *
, /
, ^
, space. came following regular expression
"([\\s\\(\\)\\-\\+\\*/\\^])"
which keeps delimiters in resulting list (which want), produces empty strings ""
elements, don't want. hardly ever use regular expression (unfortunately), not sure if possible avoid this.
here's example of problem:
>>> import re >>> e = "((12*x^3+4 * 3)*3)" >>> re.split("([\\s\\(\\)\\-\\+\\*/\\^])", e) ['', '(', '', '(', '12', '*', 'x', '^', '3', '+', '4', ' ', '', ' ', '', ' ', '', '*', '', ' ', '3', ')', '', '*', '3', ')', '']
is there way not produce empty strings, maybe modifying regular expression? of course can remove them using example filter, idea not produce them @ all.
edit
i need not include spaces. if can in matter, great.
you add \w+
, remove \s , findall:
import re e = "((12*x^3+44 * 3)*3)" print re.findall("(\w+|[()\-+*/^])", e)
output:
['(', '(', '12', '*', 'x', '^', '3', '+', '44', '*', '3', ')', '*', '3', ')']
depending on want can change regex:
e = "((12a*x^3+44 * 3)*3)" print re.findall("(\d+|[a-z()\-+*/^])", e) print re.findall("(\w+|[()\-+*/^])", e)
the first considers 12a
2 strings latter one:
['(', '(', '12', 'a', '*', 'x', '^', '3', '+', '44', '*', '3', ')', '*', '3', ')'] ['(', '(', '12a', '*', 'x', '^', '3', '+', '44', '*', '3', ')', '*', '3', ')']
Comments
Post a Comment