这是我编写的示例程序,输出可能与您期望的不完全一样,请根据您的需要调整正则表达式
public static void main(String[] args) throws Exception {
Pattern pattern = Pattern.compile("[A-G]+[A-G]{0,1}");
Pattern pattern2 = Pattern.compile("[V]+[V]*");
BufferedReader br = new BufferedReader(new FileReader(new File("test.txt")));
String line;
while ((line = br.readLine()) != null) {
StringBuffer sf = new StringBuffer();
//System.out.println(line);
String[] tempArr = line.split(" ");
//System.out.println(tempArr.length);
for(String s : tempArr){
Matcher matcher = pattern.matcher(s);
Matcher matcher2 = pattern2.matcher(s);
if (matcher.find()) {
sf.append("R");
}else if(matcher2.find()) {
sf.append(" ");
}
}
System.out.println(sf);
}
br.close();
}
编辑 2
给你
public static void main(String[] args) throws Exception {
Pattern pattern = Pattern.compile("[A-G]+");
Pattern pattern2 = Pattern.compile("[V]+");
List<String[]> mat = new ArrayList<String[]>();
String[] row1 = { "A", "V", "V", "V", "VV", "V", "V", "V" };
String[] row2 = { "E", "F", "V", "E", "VF", "E", "V", "E", };
String[] row3 = { "C", "D", "V", "C", "VD", "B", "V", "C" };
String[] row4 = { "A", "A", "V", "A", "VA", "G", "V", "A" };
String[] row5 = { "V", "D", "V", "V", "VD", "E", "V", "V" };
String[] row6 = { "A", "V", "V", "A", "VV", "V", "V", "A" };
mat.add(row1);
mat.add(row2);
mat.add(row3);
mat.add(row4);
mat.add(row5);
mat.add(row6);
int rowSize = 6;
int colSize = 8;
String[][] matrix = mat.toArray(new String[rowSize][colSize]);
for (int i = 0; i < colSize; i++) {
StringBuffer sf = new StringBuffer();
for (int j = 0; j < rowSize; j++) {
sf.append(matrix[j][i]);
}
Matcher matcher = pattern.matcher(sf.toString());
Matcher matcher2 = pattern2.matcher(sf.toString());
if (matcher.find()) {
System.out.println("R");
} else if (matcher2.matches()) {
System.out.println(" ");
}
}
}
结果:
R
R
R
R
R
R