Java替换所有问题

时间:2010-11-12 05:14:53

标签: java regex replaceall

我正在编写一个程序来解决后缀问题。我不需要帮助,因为我已经知道了。但我偶然发现了替换所有功能的问题 在这个程序中,我们给出了应该定义的运算符;我将定义存储在地图中。 E表示此问题是评估,D表示此问题是定义。这些定义可以互相嵌套。当我尝试使用replaceAll函数检索定义时出现问题。除了一个实例外,它可以工作。我会给输入文件和输出显示我的意思。

import java.io.*;
import java.util.*;

class Problem
{

    private static String line;
    private static HashMap<String, String> map = new HashMap();
    private static Stack operandStack = new Stack();

    public static void main(String[] args) throws IOException
    {

         FileReader fin = new FileReader("postfix.in");
        BufferedReader infile = new BufferedReader(fin);

        FileWriter fout = new FileWriter("postfix.out");
        BufferedWriter outfile = new BufferedWriter(fout);

        line = infile.readLine();
        do
        {

            if(line.substring(0,1).equals("E"))
            {

                line = line.replaceAll("E", "");
               line =  line.replaceAll("\"+", "");
               for(int i = 0; i < line.length(); i++)
               {

                   if(map.containsKey(line.substring(i,i+1)) ||
                       line.substring(i, i+1).matches("!|-|!|%|/"))
                   {
                       //check to see if its not a predifined operator
                       if(!line.substring(i, i+1).matches("!|-|!|%|/"))
                       {
                           String operator;

                         operator = line.substring(i, i+1);

                         //simplifies operators
                         line = line.replaceAll("\\"+operator, map.get(operator));
                       }

                   }
                   else if(!line.substring(i,i+1).equals(" "))
                   {

                        operandStack.push(line.substring(i,i+1));
                   }

               }

               System.out.println(line);
               operandStack.clear();

            }
            else if(line.substring(0,1).equals("D"))
            {
                line = line.replaceAll("\"$", "");     //remove quote at end of string
                map.put(line.substring(1,2), line.substring(3)); //put the definition on the map
            }

       //    System.out.println(map);
            line = infile.readLine();
       }while(!line.equals("Q"));

        infile.close();
        outfile.close();


    }




}

这是输入文件

D+" 0%--"
E"1 2+"
D*" 1%//"
E"3 4*"
D@"!!**"
E"17 4@+6*5/"
D&"!*"
D$" 1%/"
Da"$/"
D'" 0%-"
E"28 5&'-$"
E"3 4$/"
E"3 4a"
E"4 5a3/"
E"4@&"
E"4&@"
E"2!!!!****@"
E"2&&&@"
Q

输出代码

1 2 0%--
3 4 1%//
17 4!! 1%// 1%// 0%--6 1%//5/
28 5! 1%// 0%-- 1%/
3 4 1%//
3 4a      //this is not simplified
4 5a3/    //this is not simpliied
4!! 1%// 1%//! 1%//
4! 1%//!! 1%// 1%//
2!!!! 1%// 1%// 1%// 1%//!! 1%// 1%//
2! 1%//! 1%//! 1%//!! 1%// 1%//

我认为解决这个问题的方法在于修复这条线,但我不知道该如何处理它。

line = line.replaceAll("\\"+operator, map.get(operator));

1 个答案:

答案 0 :(得分:1)

看看这是否有效。替换您的相关行:

line = line.replaceAll("\\"+operator, map.get(operator));

以下内容:

line = line.replaceAll(
           Pattern.quote(operator),
           Matcher.quoteReplacement(map.get(operator)));

它产生输出:

1 2 0%--
3 4 1%//
17 4!! 1%// 1%// 0%--6 1%//5/
28 5! 1%// 0%-- 1%/
3 4 1%//
3 4$/
4 5$/3/
4!! 1%// 1%//! 1%//
4! 1%//!! 1%// 1%//
2!!!! 1%// 1%// 1%// 1%//!! 1%// 1%//
2! 1%//! 1%//! 1%//!! 1%// 1%//

这似乎没问题,但我没有广泛关注它是否正确。