在 java 中将中缀字符串拆分为字符串数组



我正在研究一个迷你科学计算器,它可以infixpostfix算法。我的输入是一个中缀字符串..我对postfix转换逻辑的infix需要string array.那么我怎样才能像这样拆分中缀字符串:

 100+(0.03*55)/45-(25+55)

到字符串数组,其中每个操作数和运算符都是一个数组元素。 像这样

 "100" , "+" , "(" , "0.03" , "*" , "55" , ")" , "/" , "45" , "-"

等等...

请注意,字符串中没有空格,因此不能在正则表达式" "的基础上拆分它。

显然,每个字符都是一个单独的标记,除了可能带有点的连续数字。因此,一个简单的解决方案是遍历字符串,然后当您看到一个数字前面有另一个数字(或小数点分隔符,一个点)时,您将字符添加到上一个标记中,否则将其添加到新标记中。

这里的代码:

public static List<String> getTokens(String inputString) {
    List<String> tokens = new ArrayList<String>();
    // Add the first character to a new token. We make the assumption
    // that the string is not empty.
    tokens.add(Character.toString(inputString.charAt(0)));
    // Now iterate over the rest of the characters from the input string.
    for (int i = 1; i < inputString.length(); i++) {
        char ch = inputString.charAt(i); // Store the current character.
        char lch = inputString.charAt(i - 1); // Store the last character.
        // We're checking if the last character is either a digit or the
        // dot, AND if the current character is either a digit or a dot.
        if ((Character.isDigit(ch) || ch == '.') && (Character.isDigit(lch) || lch == '.')) {
            // If so, add the current character to the last token.
            int lastIndex = (tokens.size() - 1);
            tokens.set(lastIndex, tokens.get(lastIndex) + ch);
        }
        else {
            // Otherwise, add the current character to a new token.
            tokens.add(Character.toString(ch));
        }
    }
    return tokens;
}

请注意,此方法比大多数正则表达式方法更快

您可以使用正则表达式来解析存储在字符串中的数学表达式。

expString.split("(?<=[-+*/\(\)])|(?=[-+*/\(\)])");

会为你做这个伎俩。

String str = "100+(0.03*55)/45-(25+55)";
String[] outs = str.split("(?<=[-+*/\(\)])|(?=[-+*/\(\)])");
for (String element : outs)
{
    System.out.println(element);
}

会给你一个输出,

100
+
(
0.03
*
55
)
/
45
-
(
25
+
55
)

请检查我的实验@http://rextester.com/QEMOYL38160

这是我将使用的算法:

从空字符串

数组和空字符串缓冲区开始

  • 从字符 0 走到字符 N
  • 对于当前字符,确定类型(数字/句点、左括号、闭括号、数学运算符)
  • 如果当前字符类型与最后一个字符类型相同
  • 将当前字符添加到缓冲区
  • 如果不一样,则将缓冲区放入字符串数组中,然后启动一个新的缓冲区

您需要使用前瞻和后视与拆分。

这行得通。当然,如果您想包含更多元素,请改进正则表达式。

public static void main(String[] args) {
    String input = "100+(0.03*55)/45-(25+55)";
    String test[] = input.split("((?<=[\+\-\*\/\(\)\{\}\[\]])|(?=[\+\-\*\/\(\)\{\}\[\]]))");
    System.out.println(Arrays.toString(test));
}

更新:

((?<=[a-z]]) ,表示它将基于任何字符进行拆分,并将该字符包含在拆分数组中以及元素之后。

(?=[a-z]) ,表示它将基于任何字符进行拆分,并将该字符包含在每个元素之前的拆分数组中。

| ,是两个正则表达式之间的 or 运算符。

[\+\-\*\/\(\)\{\}\[\]]) ,是匹配可能组合的正则表达式

请看一下另一个问题的答案。

这应该可以解决问题:

Pattern p = Pattern.compile("(?:(\d+)|([+-*/\(\)]))");
Matcher m = p.matcher("100+(0.03*55)/45-(25+55)");
List<String> tokens = new LinkedList<String>();
while(m.find())
{
  String token = m.group( 0 ); //group 0 is always the entire match   
  tokens.add(token);
}

最新更新