大概意识是这样的 我用个例子说明! 11 
00:00:56,822 --> 00:01:01,259 
这样武功的精髓才能通过你再到他们,永存下去…… 12 
00:01:04,530 --> 00:01:06,521 
- ……他们都是可爱的孩子。 
- 什么? 13 
00:01:06,599 --> 00:01:08,123 
- 祝你好运。 
- 等等! 14 
00:01:08,200 --> 00:01:10,794 
师傅!不要啊! 15 
00:01:15,141 --> 00:01:19,441 
好吧,请你们先坐好。 这是一个字幕文件中的内容。。我想把里面的汉字抽出来。。并且对应好时间。然后存到数据库中注意。。汉字有一行的也有两行的 在数据库中的意思如下 
id time content 
14 00:01:08,200 --> 00:01:10,794 师傅! 不要啊! 
希望高手能给予解答小弟我先谢谢啦

解决方案 »

  1.   

    这个不需要用正则表达式吧等于所有的内容需要存入数据表中,每个时间片段当中都有个空行的,遇到空行就是新的一条记录
    id 和 time 是头两行,余下的全是 content。
      

  2.   

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;public class Test {
        
        private static final String LINE_SEPARATOR = System.getProperty("line.separator");    public List<LrcSeg> readFileToLrc(String filename) throws IOException {
            BufferedReader br = null;
            List<LrcSeg> segs = new ArrayList<LrcSeg>();
            try {
                br = new BufferedReader(new FileReader(filename));
                for(String str = null; (str = br.readLine()) != null; ) {
                    str = str.trim();
                    if(str.length() == 0) {
                        continue;
                    }
                    LrcSeg seg = new LrcSeg();
                    seg.setId(Integer.parseInt(str));
                    
                    str = br.readLine();
                    if(str == null) {
                        segs.add(seg);
                        return segs;
                    }
                    seg.setTime(str.trim());
                    
                    StringBuilder sb = new StringBuilder();
                    int offset = 0;
                    while((str = br.readLine()) != null && str.trim().length() > 0) {
                        if(offset++ > 0) {
                            sb.append(LINE_SEPARATOR);
                        }
                        sb.append(str.trim());
                    }
                    seg.setContent(sb.toString());
                    segs.add(seg);
                }
            } finally {
                if(br != null) {
                    br.close();
                }
            }
            return segs;
        }
        public static void main(String[] args) throws IOException {
            Test t = new Test();
            List<LrcSeg> segs = t.readFileToLrc("d:/wy.txt");
            
            for(int i = 0, k = segs.size(); i < k; i++) {
                System.out.println(segs.get(i));
            }
        }
    }class LrcSeg {
        private int id;
        private String time;
        private String content;
        
        public LrcSeg() {        
        }    
        public LrcSeg(int id, String time, String content) {
            this.id = id;
            this.time = time;
            this.content = content;
        }
        public String getContent() {
            return content;
        }
        public void setContent(String content) {
            this.content = content;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getTime() {
            return time;
        }
        public void setTime(String time) {
            this.time = time;
        }    public String toString() {
            return  "     ID: " + this.id + "\n" +
                    "   TIME: " + this.time + "\n" +
                    "CONTENT: " + this.content + "\n";
        }
    }