将一个保存有ip地址与地区对照关系的文本文件导入到数据库时,应该将其中的某些空格替换成逗号(,),即对于如下格式的文本文件内容:
        起始IP                   结束IP                      地区
        61.54.231.245         61.54.231.245          河南省安阳市 新世纪网吧
        61.54.231.246         61.54.231.246          河南省安阳市 未知地区
        61.54.231.9             61.54.231.247          河南省安阳市 红日网吧
        61.54.231.248          61.54.231.248          河南省安阳市 安阳师范学院
        61.54.231.249          61.54.231.249         河南省安阳市 黑蜘蛛网吧(师范学院附近)
应转换成下面的这种格式:
        61.54.231.245,61.54.231.245 ,河南省安阳市 新世纪网吧
        61.54.231.246,61.54.231.246,河南省安阳市 未知地区
        61.54.231.9,61.54.231.247 ,河南省安阳市 红日网吧
        61.54.231.248,61.54.231.248,河南省安阳市 安阳师范学院
        61.54.231.249,61.54.231.249,河南省安阳市 黑蜘蛛网吧(师范学院附近)编写一个java程序来自动实现上面的正则表达式替换,将a.txt(下载)中的IP地址数字后的空格替换成“,”号后,将替换结果保存到b.txt文件中。package Test8;import java.io.*;import java.util.regex.Matcher;
import java.util.regex.Pattern;public class Test8 { /**
 * @param args
 */
public static void main(String[] args) {
File inf = new File("F:/c.txt");
File outf = new File("F:/d.txt");
lanuch(inf, outf); }

public static void lanuch(File inf, File outf){
FileReader fr = null;
try {
fr = new FileReader(inf);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader br = new BufferedReader(fr);

FileWriter fw = null;
try {
fw = new FileWriter(outf);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
BufferedWriter bw = new BufferedWriter(fw);

String inStr = "";
String outStr = "";
int inCount = 0;
int outCount = 0;
try {
while(null != (inStr = br.readLine())){
inCount ++;
String strIP = "([\\d]{1,3}\\.[\\d]{1,3}\\.[\\d]{1,3}\\.[\\d]{1,3})" +
"([\\s]*)";
Pattern p = Pattern.compile(strIP);
Matcher m = p.matcher(inStr);
if(m.find()){
outStr = inStr.replaceAll(m.group(2), ",");

bw.write(outStr);
bw.newLine();
outCount ++;
}
}
System.out.println(inCount);
System.out.println(outCount);
} catch (IOException e) {
e.printStackTrace();
}
}}

解决方案 »

  1.   

    结果总是有的能替换,有的不能,迷惑中61.54.235.232   61.54.243.196   河南省安阳市 网通ADSL
    61.54.243.197   61.54.243.197   河南省安阳市 世纪商务公司
    61.54.243.198   61.54.243.200   河南省安阳市 网通ADSL
    61.54.243.201   61.54.243.201   河南省安阳市 通海网吧(师范附近)
    61.54.243.202   61.54.245.244   河南省安阳市 网通ADSL
    61.54.245.245   61.54.245.245   河南省安阳市 景霖网吧
    61.54.245.246   61.54.245.255   河南省安阳市 网通ADSL
    61.54.246.0     61.54.253.27    河南省鹤壁市  未知地区
    61.54.253.28    61.54.253.28    河南省鹤壁市 奔流街七喜电脑公司
    61.54.253.29    61.54.253.255   河南省鹤壁市  未知地区
    61.54.254.0     61.54.254.172   河南省漯河市  未知地区
    61.54.254.173   61.54.254.173   河南省漯河市 天乐网吧
    61.54.254.174   61.54.254.174   河南省漯河市 干河陈金太阳网吧
    61.54.254.175   61.54.255.255   河南省漯河市 网通
    61.55.0.0       61.55.3.13      河北省石家庄市 网通ADSL
    61.55.3.14      61.55.3.14      河北省石家庄市 行唐县玉城街科海网吧
    61.54.235.232,61.54.243.196,河南省安阳市 网通ADSL
    61.54.243.197,61.54.243.197,河南省安阳市 世纪商务公司
    61.54.243.198,61.54.243.200,河南省安阳市 网通ADSL
    61.54.243.201,61.54.243.201,河南省安阳市 通海网吧(师范附近)
    61.54.243.202,61.54.245.244,河南省安阳市 网通ADSL
    61.54.245.245,61.54.245.245,河南省安阳市 景霖网吧
    61.54.245.246,61.54.245.255,河南省安阳市 网通ADSL
    61.54.246.0,61.54.253.27    河南省鹤壁市  未知地区
    61.54.253.28,61.54.253.28,河南省鹤壁市 奔流街七喜电脑公司
    61.54.253.29,61.54.253.255   河南省鹤壁市  未知地区
    61.54.254.0,61.54.254.172   河南省漯河市  未知地区
    61.54.254.173,61.54.254.173,河南省漯河市 天乐网吧
    61.54.254.174,61.54.254.174,河南省漯河市 干河陈金太阳网吧
    61.54.254.175,61.54.255.255,河南省漯河市 网通
    61.55.0.0,61.55.3.13      河北省石家庄市 网通ADSL
    61.55.3.14,61.55.3.14,河北省石家庄市 行唐县玉城街科海网吧
    61.55.3.15,61.55.3.255     河北省石家庄市 网通ADSL
    61.55.4.0,61.55.4.181     河北省石家庄市 桥西区网通ADSL
    61.55.4.182,61.55.4.182,河北省石家庄市 职业技术学院科技活动中心
      

  2.   

    我调了下,把你的if(m.find()),改成while,然后输出拿出来另外加了bw.close();没有这句你能输出到文件?try {
                while(null != (inStr = br.readLine())){
                    inCount ++;
                    String strIP = "([\\d]{1,3}\\.[\\d]{1,3}\\.[\\d]{1,3}\\.[\\d]{1,3})" +
                            "([\\s]*)";
                    Pattern p = Pattern.compile(strIP);
                    Matcher m = p.matcher(inStr);
                    while(m.find()){
                        outStr = inStr.replaceAll(m.group(2), ",");
                    
                        inStr=outStr;
                    }     
                    bw.write(outStr);
                    bw.newLine();
                    outCount ++;
                }
                bw.close();
                System.out.println(inCount);
                System.out.println(outCount);
            } catch (IOException e) {
                e.printStackTrace();
            }
      

  3.   

    我调了下,把你的if(m.find()),改成while,然后输出拿出来另外加了bw.close();没有这句你能输出到文件?try {
                while(null != (inStr = br.readLine())){
                    inCount ++;
                    String strIP = "([\\d]{1,3}\\.[\\d]{1,3}\\.[\\d]{1,3}\\.[\\d]{1,3})" +
                            "([\\s]*)";
                    Pattern p = Pattern.compile(strIP);
                    Matcher m = p.matcher(inStr);
                    while(m.find()){
                        outStr = inStr.replaceAll(m.group(2), ",");
                    
                        inStr=outStr;
                    }     
                    bw.write(outStr);
                    bw.newLine();
                    outCount ++;
                }
                bw.close();
                System.out.println(inCount);
                System.out.println(outCount);
            } catch (IOException e) {
                e.printStackTrace();
            }
      

  4.   

    LZ试试看
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.regex.*;public class MyRegex {
    public static void main(String[] args) {
    String temp = null;
    try {
    // 读取文件内容
    String s = read("E:/2.txt");
    // 将多空格变成一个空格
    s = s.replaceAll(" {2,}", " ");
    // 将开头的空格去掉
    s = s.replaceAll("(?m)^ ", "");
    // 将每行末尾的空格去掉
    s = s.replaceAll("(?m) $", "");
    Matcher m = Pattern.compile("\\d \\d.*\\d \\S").matcher(s);
    StringBuffer buf = new StringBuffer();
    while (m.find()) {
    temp = m.group();
    temp = temp.replaceAll(" ", ",");
    m.appendReplacement(buf, temp);
    }
    m.appendTail(buf);
    System.out.println(buf);
    } catch (IOException e) {
    e.printStackTrace();
    }
    } public static String read(String fileName) throws IOException {
    BufferedReader br = new BufferedReader(new FileReader(fileName));
    StringBuilder sb = new StringBuilder();
    String s;
    while ((s = br.readLine()) != null) {
    sb.append(s);
    sb.append("\r\n");
    }
    br.close();
    return sb.toString();
    }
    }
      

  5.   

    主要是这段有点绕:
    //筛选出前面的两个字符串
    Matcher m = Pattern.compile("\\d \\d.*\\d \\W").matcher(s);
    StringBuffer buf = new StringBuffer();
    while (m.find()) {
    //得到有前面两个空格的字符串
    temp = m.group();
    //把前面的两个字符串中的空格换成逗号
    temp = temp.replaceAll(" ", ",");
    //追加到buf中去
    m.appendReplacement(buf, temp);
    }
    //加上末尾的字符串
    m.appendTail(buf);
    这是比较笨的方法
      

  6.   


    没有bw.close();可以输出到文件,但是输出不完整,很奇怪呀
      

  7.   

    完成了:
    package Test8;import java.io.*;import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class Test8 { public static void main(String[] args) {
    File inf = new File("E:/a.txt");
    File outf = new File("E:/b.txt");
    lanuch(inf, outf); }

    public static void lanuch(File inf, File outf){
    FileReader fr = null;
    try {
    fr = new FileReader(inf);
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }
    BufferedReader br = new BufferedReader(fr);

    FileWriter fw = null;
    try {
    fw = new FileWriter(outf);
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    BufferedWriter bw = new BufferedWriter(fw);

    String inStr = "";
    String outStr = "";

    int inCount = 0;
    try {
    //任务一:
    while(null != (inStr = br.readLine())){
    inCount ++;
    String strIP = "([\\d]{1,3}\\.[\\d]{1,3}\\.[\\d]{1,3}\\.[\\d]{1,3})([\\s]+)";
    Pattern p = Pattern.compile(strIP);
    Matcher m = p.matcher(inStr);
    while(m.find()){
    outStr = inStr.replaceFirst(m.group(2), ",");
    inStr = outStr;
    }
    /* //任务二:
    StringBuffer sb = new StringBuffer();
    Pattern p1 = Pattern.compile("[\\d]{1,3}[.,]");
    Matcher m1 = p1.matcher(outStr);
    while(m1.find()){
    if(2 == m1.group().length()){
    m1.appendReplacement(sb, "00" + m1.group());
    }else{
    if(3 == m1.group().length()){
    m1.appendReplacement(sb, "0" + m1.group());
    }else{
    continue;
    }
    }
    }
    m1.appendTail(sb);

    bw.write(sb.toString());*/
    bw.write(outStr);
    bw.newLine();
    }
    bw.close();
    System.out.println(inCount);
    } catch (IOException e) {
    e.printStackTrace();
    }
    }}
      

  8.   

    答:lanuch可简单化处理。如下:public static void lanuch(File inf, File outf){
            FileReader fr = null;
            try {
                fr = new FileReader(inf);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            BufferedReader br = new BufferedReader(fr);
            
            FileWriter fw = null;
            try {
                fw = new FileWriter(outf);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            BufferedWriter bw = new BufferedWriter(fw);
            
            String inStr = "";
            String outStr = "";
            int inCount = 0;
            int outCount = 0;
            try {
                while(null != (inStr = br.readLine())){
                    inCount ++;
                    outStr=inStr.replaceAll("(?<=\\d)\\s+", ","); //用这个来直接替换吧
                    bw.write(outStr);
                    bw.newLine();
                    outCount ++;            }
                bw.flush();
                bw.close();
                System.out.println(inCount);
                System.out.println(outCount);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
      

  9.   


    outStr=inStr.replaceAll("(?<=\\d)\\s+", ","); //用这个来直接替换吧
     
    高手呀,太强了