replaceAll()正则表达式 想把文件的所有行号去掉,例如0021: ,就是4个数字+冒号,replaceAll的参数该怎么表示呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 是全部都是這種格式?那 ^[0-9][0-9][0-9][0-9]\:$先把0021:拿出來和他match一下 然后true的話就replace 比如用substring()切一下或者split()之類的又不是驗證 干什么非要正則啊 用 string.replaceAll("\d{4}:" , ""); , 提示invalid escape sequence, 怎么回事? 不用正则的话,System.out.println(str.substring(5));用的话,System.out.println(str.replaceAll("\\d{4}:",""));你得少了个\ import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.Closeable;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Test { public static void main(String[] args) { BufferedReader br = null; BufferedWriter bw = null; try { Pattern pattern = Pattern.compile("(?m)^[ \t]*\\d{4}:[ \t]*"); String line = System.getProperty("line.separator"); br = new BufferedReader(new FileReader("1.txt")); bw = new BufferedWriter(new FileWriter("1_.txt")); String str = null; while((str = br.readLine())!=null) { str = replace(pattern, str); bw.write(str + line); } }catch(IOException e) { e.printStackTrace(); }finally{ close(bw); close(br); } System.out.println("finished."); } private static String replace(Pattern pattern, String str) { Matcher matcher = pattern.matcher(str); return matcher.replaceAll(""); } private static void close(Closeable closeable) { if(closeable != null) { try { closeable.close(); } catch (IOException e) { e.printStackTrace(); } } }}文件内容0001: Line 1, 0001: Line 1 0002: Line 2, 0002: Line 2 0003: Line 3, 0003: Line 3 0004: Line 4, 0004: Line 4 100分求SCJP 1.4(310-025,SL-275) 中文题库 网络通信设计问题 高手进来讨论一下垃圾回收 怎么改变JButton中文字的颜色? 关于Runtime.getRuntime().exec,为什么下面的程序总是运行失败? hibernate基础配置问题(参考apress beginning Hibernate) Applet能从远端服务器加载多大的文件?? 想学servlet,不知道要下什么工具,谁来拿分? Java的路该怎么走...头疼!求指导 JAVA第一个程序,HelloWorld Hashtable的问题. 本人公司碰到一个有关内存的极难问题,望大虾赐教。
那 ^[0-9][0-9][0-9][0-9]\:$
先把0021:拿出來和他match一下 然后true的話就replace
又不是驗證 干什么非要正則啊
用的话,System.out.println(str.replaceAll("\\d{4}:",""));
你得少了个\
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class Test { public static void main(String[] args) {
BufferedReader br = null;
BufferedWriter bw = null;
try {
Pattern pattern = Pattern.compile("(?m)^[ \t]*\\d{4}:[ \t]*");
String line = System.getProperty("line.separator");
br = new BufferedReader(new FileReader("1.txt"));
bw = new BufferedWriter(new FileWriter("1_.txt"));
String str = null;
while((str = br.readLine())!=null) {
str = replace(pattern, str);
bw.write(str + line);
}
}catch(IOException e) {
e.printStackTrace();
}finally{
close(bw);
close(br);
}
System.out.println("finished.");
}
private static String replace(Pattern pattern, String str) {
Matcher matcher = pattern.matcher(str);
return matcher.replaceAll("");
}
private static void close(Closeable closeable) {
if(closeable != null) {
try {
closeable.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}文件内容0001: Line 1, 0001: Line 1
0002: Line 2, 0002: Line 2
0003: Line 3, 0003: Line 3
0004: Line 4, 0004: Line 4