How can I split a boolean expression in Java? For example, I want to get the following from the expression a_1 & b_2 | (!c_3):
String tokens[] = {"a_1", "&", "b_2", "|", "(", "!", "c_3", ")"};
The names of the variables contain alphanumeric characters and underscores (_).
If you have
first, get rid of spaces:
then, obtain the array you want like this:
Update: based on OP’s changed question, another solution follows: