How can tokenize this string in java?

后端 未结 9 1075
你的背包
你的背包 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:41

    Here is my tokenizer solution that allows for negative numbers (unary).

    So far it has been doing everything I needed it to:

    private static List tokenize(String expression)
        {
            char c;
            List tokens = new ArrayList();
            String previousToken = null;
            int i = 0;
            while(i < expression.length())
            {
                c = expression.charAt(i);
                StringBuilder currentToken = new StringBuilder();
    
                if (c == ' ' || c == '\t') // Matched Whitespace - Skip Whitespace
                {
                    i++;
                }
                else if (c == '-' && (previousToken == null || isOperator(previousToken)) && 
                        ((i+1) < expression.length() && Character.isDigit(expression.charAt((i+1))))) // Matched Negative Number - Add token to list
                {
                    currentToken.append(expression.charAt(i));
                    i++;
                    while(i < expression.length() && Character.isDigit(expression.charAt(i)))
                    {
                        currentToken.append(expression.charAt(i));
                        i++;
                    }   
                }
                else if (Character.isDigit(c)) // Matched Number - Add to token list
                {
                    while(i < expression.length() && Character.isDigit(expression.charAt(i)))
                    {
                        currentToken.append(expression.charAt(i));
                        i++;
                    }
                }
                else if (c == '+' || c == '*' || c == '/' || c == '^' || c == '-') // Matched Operator - Add to token list
                {
                    currentToken.append(c);
                    i++;
                }
                else // No Match - Invalid Token!
                {
                    i++;
                }
    
                if (currentToken.length() > 0)
                {
                    tokens.add(currentToken.toString());    
                    previousToken = currentToken.toString();    
                }
            }   
            return tokens;
        }
    

提交回复
热议问题