java.util.regex.PatternSyntaxException:null(在java.util.regex.Pattern中)错误

时间:2011-02-07 01:06:42

标签: java string character

public void convertStrings() {
    for (int counter = 0; counter < compare.length; counter++) {
            compare[counter] = compare[counter].replace('*','_'); 
            compare[counter] = compare[counter].replaceAll("_",".*"); 
            compare[counter] = compare[counter].replace('?', '.'); 
    }
    // System.out.printf("%s", Arrays.toString(compare));
} 
public void compareStrings() {
    for (int counter = 0; counter < data.length; counter++) {
        for (int counter1 = 0; counter1 < compare.length; counter1++) {

            if (data[counter].matches(compare[counter1]) == true) {
                System.out.printf("%s ", data[counter]); 
            }

        }
        System.out.println(); 
    }
}

}

我要做的是将输入中的任何*替换为。*,这样当我将字符串与之前的任何内容进行比较时,它将忽略以前的字符。另外,我正在转换一个“?”到占位符值“。”。但是,当我运行已编译的代码时,我收到此错误,因为该字符串将特殊字符转换为常规字母。如何让编译器注册这些特殊字符来执行该功能?

2 个答案:

答案 0 :(得分:1)

只需将行更改为:

compare[counter] = compare[counter].replaceAll("\\*",".*").replaceAll("\\?", "."); 

答案 1 :(得分:0)

我假设从行中抛出错误

if (data[counter].matches(compare[counter1]) == true)

如果是这样,最可能的解释是compare[counter1]实际上是null,也就是说它不包含值。