import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestPattern {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("/home/eagle/work/pa"));
String line = br.readLine();
Pattern p = Pattern
.compile("([0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3})|(\\d+)");
while (line != null) {
Matcher m = p.matcher(line);
if (m.matches()) {
System.out.println("Match!" + line);
line = br.readLine();
continue;
} else {
line = br.readLine();
}
}
}
}
运行结果是:
Match!123
Match!00:00:00,234
我也不知道你的ubuntu是为什么了,我也是ubuntu
如果你的行中包含这个那就这个么写就可以了:
Pattern p = Pattern
.compile("(.+?[0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3}.+?)|(.+?\\d.+?)");
其他不变,结果是
Match!ad123sdf
Match!dsf00:00:00,234asdf