主要处理nz i l
public static void main(String[] args) { // TODO Auto-generated method stub StringBuffer sb = new StringBuffer(); String tempstr = null; try { FileWriter fp = new FileWriter("E:/text/destext.txt"); String path = "E:/text/srctext.txt"; File file = new File(path); if (!file.exists()) throw new FileNotFoundException(); Pattern pattern1 = Pattern.compile("\\[.*?]n"); Pattern pattern0 = Pattern.compile("\\[.*?]i"); Pattern pattern2 = Pattern.compile("\\[.*?]l"); Pattern pattern3 = Pattern.compile("\\[.*?]nz"); FileInputStream fis = new FileInputStream(file); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); while ((tempstr = br.readLine()) != null) { String line = ""; String line1 = ""; // sb.append(tempstr); Matcher matcher = pattern1.matcher(tempstr); while (matcher.find()) { String src = ""; String tmp = ""; for (int j = 0; j <= matcher.groupCount(); j++) { // System.out.print( matcher.group(j) +" "); src = matcher.group(j); tmp = matcher.group(j); tmp = tmp.replaceAll("/\\w+", ""); tmp = tmp.replaceAll(" ", ""); tmp = tmp.replaceAll("\\[", ""); tmp = tmp.replaceAll("\\]", "/"); System.out.println(tmp); } tempstr = tempstr.replace(src, tmp); System.out.println(); } Matcher matcher1 = pattern0.matcher(tempstr); while (matcher1.find()) { String src = ""; String tmp = ""; for (int j = 0; j <= matcher1.groupCount(); j++) { src = matcher1.group(j); tmp = matcher1.group(j); tmp = tmp.replaceAll("/\\w+", ""); tmp = tmp.replaceAll(" ", ""); tmp = tmp.replaceAll("\\[", ""); tmp = tmp.replaceAll("\\]", "/"); System.out.println(tmp); } tempstr =tempstr.replace(src, tmp); System.out.println(); } Matcher matcher2 = pattern2.matcher(tempstr); while (matcher2.find()) { String src=""; String tmp =""; for (int j = 0; j <= matcher2.groupCount(); j++) { // System.out.print( matcher.group(j) +" "); src = matcher2.group(j); tmp= matcher2.group(j); tmp = tmp.replaceAll("/\\w+", ""); tmp = tmp.replaceAll(" ", ""); tmp = tmp.replaceAll("\\[", ""); tmp = tmp.replaceAll("\\]", "/"); System.out.println(tmp); } tempstr =tempstr.replace(src, tmp); System.out.println(); } fp.write(tempstr + "\n"); } fp.close(); } catch (IOException ex) { System.out.println(ex.getStackTrace()); } }