How to Split a mathematical expression on operators as delimiters, while keeping them in the result?

前端 未结 5 1050
忘了有多久
忘了有多久 2020-11-29 09:36

I need to split an expression like

a+b-c*d/e 

and get a, b, c, d, e sepe

5条回答
  •  臣服心动
    2020-11-29 10:29

    I think what you want to do, is more like a parser, rather than a tokenizer.

    With a string tokenizer, you usually have a long string (i.e. "parameter1;parameter2;parameter3") with several elements concatenated, and using a "token" to separate these elements. With function "String.split", you are basically saying: "Ok, give all elements from this string, but taking into account that character ';' separates different elements". And then you get "parameter1", "parameter2", "parameter3". You do not care about separators.

    But in a mathematical expression like yours: "a+b-c*d/e", you want to get all the individual elements (operands and operators). Here there are no explicit separators, and the order is also important for you. I would use a parser library and write a small grammar instead.

提交回复
热议问题