temp.matches 即使存在正确的匹配表达式,此正则表达式也会返回false( SOUTH,NORTH,)
执行后的临时值 SOUTH,NORTH,
import java.util.Arrays;
public class DirReduction {
public static String[] dirReduc(String[] arr) {
boolean check=false;
String inputToString =Arrays.toString(arr);
String str1 = inputToString.replace("]", ", ").replace("[", ""),temp;
do {
temp=str1.replaceAll("NORTH, SOUTH, " , "").replaceAll("SOUTH, NORTH, " , "").replaceAll("EAST, WEST, " , "").replaceAll("WEST, EAST, " , "");
check = temp.matches(".*\\b(NORTH, SOUTH, |SOUTH, NORTH, |EAST, WEST, |WEST, EAST, )\\b.*");
System.out.println(temp);
str1=temp;
}while(check);
String[] splitArray = str1.split(", ");
String[] fi =new String[splitArray.length];
int i=0;
for(String item:splitArray) {
fi[i++]=item;
}
System.out.println(Arrays.toString(fi));
return fi;
}
public static void main(String[] args) {
String[] input = new String[] { "NORTH","SOUTH","SOUTH","EAST","WEST","NORTH"};
dirReduc(input);
}
}
答案 0 :(得分:0)
我假设你的replaceAll语句没有替换任何东西。您的输入字符串在逗号后面没有空格,但您的搜索掩码有空格。所以什么都不会被替换。