简体   繁体   中英

java.util.regex.PatternSyntaxException: null (in java.util.regex.Pattern) error

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(); 
    }
}

}

what i'm trying to do is replace any * in my input to a .* so that when i compare the string to anything before, it'll ignore previous characters. Also, i'm converting a "?" into a placeholder value ".". However, when i run the compiled code, I get this error because the string converts the special characters into regular letters. How do i make the compiler register these special characters to perform the function?

只需将行更改为:

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

I am assuming that the error is being thrown from the line

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

If so, the most likely explanation is that compare[counter1] is actually null , that is it does not contain a value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM