How can tokenize this string in java?

后端 未结 9 1139
你的背包
你的背包 2021-01-14 17:32

How can I split these simple mathematical expressions into seperate strings?

I know that I basically want to use the regular expression: \"[0-9]+|[*+-^()]\"

9条回答
  •  温柔的废话
    2021-01-14 17:46

    You can't use String.split() for that, since whatever characters match the specified pattern are removed from the output.

    If you're willing to require spaces between the tokens, you can do...

    "578 + 223 - 5 ^ 2 ".split(" ");
    

    which yields...

    578
    +
    223
    -
    5
    ^
    2
    

提交回复
热议问题