日期:2014-05-20  浏览次数:20600 次

正则问题!头糊了。
http://sy3.cietv.com:554/live.soooner.com/5 CCTV-1 综合频道
http://sy3.cietv.com:554/live.soooner.com/5 CCTV-1 综合频道
。。。


比如上面这里的一个文件,我只想让它变成这样的:

FILE=http://sy3.cietv.com:554/live.soooner.com/5
TITLE=CCTV-1 综合频道
FILE=http://sy3.cietv.com:554/live.soooner.com/5
TITLE=CCTV-1 综合频道
。。。。

哪位高人做的到,还请指点一二啊。

------解决方案--------------------
Java code

public static void main(String[] args) throws Exception{
        FileOutputStream dstout = new FileOutputStream("d:/dst.txt");
        InputStream in = new FileInputStream("d:/src.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String line = "";
        while((line=br.readLine())!=null){
            System.out.println(line);
            String lines[] = line.split("\\s",2);
            dstout.write(("FILE="+lines[0]).getBytes());
            dstout.write("\r\n".getBytes());
            dstout.write(("TITLE="+lines[1]).getBytes());
            dstout.write("\r\n".getBytes());
        }
        in.close();
        br.close();
        dstout.close();
    }

------解决方案--------------------
Java code
    public static void main(String[] args) {
        String str = "http://sy3.cietv.com:554/live.soooner.com/5 CCTV-1 综合频道";

        String reg = "(^http://.*? )(.*)";
        Pattern p = Pattern.compile(reg);
        Matcher match = p.matcher(str);
        StringBuffer result = new StringBuffer();
        int fileIndex = 1;
        int titleIndex = 2;
        String fileUrl = null;
        while (match.find()) {
            fileUrl = match.group(fileIndex);
            fileUrl = fileUrl.substring(0, fileUrl.length() - 1);
            result.append("FILE=").append(fileUrl).append("\n");
            result.append("TITLE=").append(match.group(titleIndex))
                    .append("\n");
        }
        System.out.println(result.toString());
    }