如何用正侧来分析这样的数据?
1
00:00:00,530 --> 00:00:02,070
Previously on Prison Break
2
00:00:02,110 --> 00:00:03,500
I didn 't kill that man, michael.
3
00:00:03,800 --> 00:00:05,430
If you didn 't kill terrence steadman,
我想分析后的数据是
Object data=new Object();
data.n=1;
data.startTime=00:00:00,530;
data.endTime=00:00:02,070;
data.content= "I didn 't kill that man, michael. ";
....
如何写?谢谢
------解决方案--------------------\d\s\d{2}:\d{2}:\d{2},\d{3} --> \d{2}:\d{2}:\d{2},\d{3}\s*([^\d]*)\s*
整体对应$0,括号里对应$1
------解决方案--------------------import java.io.BufferedReader;
import java.io.FileReader;
import
java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test2 {
public static void main(String[] args) throws
IOException {
String subtitle = readFile( "pb.srt ");
String str = "(\\d+)\\s+(\\d{2}:\\d{2}:\\d{2},\\d{3}) --> (\\d{2}:\\d{2}:\\d{2},\\d{3})\\s+(.+) ";
Pattern pattern = Pattern.compile(str);
Matcher matcher = pattern.matcher(subtitle);
List <Subtitle> prisionBreak = new ArrayList <Subtitle> ();
while(matcher.find()) {
Subtitle sub = new Subtitle();
sub.setN(Integer.parseInt(matcher.group(1)));
sub.setStartTime(matcher.group(2));
sub.setEndTime(matcher.group(3));
sub.setContent(matcher.group(4));
prisionBreak.add(sub);
}
for(Subtitle sub : prisionBreak) {
System.out.println(sub.getN());
System.out.println(sub.getStartTime() + " --> " + sub.getEndTime());
System.out.println(sub.getContent());
System.out.println();
}
}
public static String readFile(String filename) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(filename));
String str = " ";
StringBuffer sb = new StringBuffer();
String line = System.getProperty( "line.separator ");
while( (str = br.readLine()) != null ) {
if(str.trim().length() > 0) {
sb.append(str).append(line);
}
}
br.close();
return sb.toString();
}
}
class Subtitle {
private int n;
private String startTime;
private String endTime;
private String content;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getEndTime() {
return endTime;
}
public void setEndTime(String endTime) {
this.endTime = endTime;
}
public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
public String getStartTime() {
return startTime;
}
public void setStartTime(String startTime) {
this.startTime = startTime;
}
}