Java String.split() Regex

后端 未结 6 608
南笙
南笙 2020-11-27 16:23

I have a string:

String str = \"a + b - c * d / e < f > g >= h <= i == j\";

I want to split the string on all of the operators

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-27 17:01

    You could also do something like:

    String str = "a + b - c * d / e < f > g >= h <= i == j";
    String[] arr = str.split("(?<=\\G(\\w+(?!\\w+)|==|<=|>=|\\+|/|\\*|-|(<|>)(?!=)))\\s*");
    

    It handles white spaces and words of variable length and produces the array:

    [a, +, b, -, c, *, d, /, e, <, f, >, g, >=, h, <=, i, ==, j]
    

提交回复
热议问题