Have you any idea about elegant splitting input String by String Array?
For example:
We have splitter array with String elements
["=",",=","<=",">=","<",">"]
We have an expression
"44+22<=x+y>=2x+4r>34"
The result should be:
["44+22","x+y", "2x+4r","34"]
Use the regex, (\\<\\=)|(\\>\\=)|(\\>)
where |
is used as or
.
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String exp = "44+22<=x+y>=2x+4r>34";
String[] arr = exp.split("(\\<\\=)|(\\>\\=)|(\\>)");
System.out.println(Arrays.toString(arr));
}
}
Output:
[44+22, x+y, 2x+4r, 34]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.