I have Java strings which are boolean expressions with parentheses, &
, |
, and !
as operators, and I want to split them into tokens. For example:
((!A1)&(B2|C3))
should become "(","(","!","A1",")","&","(","B2","|","C3",")",")"
Following this answer I found that I can use Java's String.split()
with a regex that includes lookahead and lookbehind clauses:
List<String> tokens = "((!A1)&(B2|C3))".split("((?<=[!&()|])|(?=[!&()|]))")
My only problem is that whitespace will be included in the list of tokens. For example if I were to write the expression as ( ( !A1 ) & ( B2 | C3 ) )
then my split()
would produce at least four strings like " "
and there'd be padding around my variables (e.g. " A1 "
).
How can I modify this split
expression and regex to tokenize the string but not keep any of the witespace?