Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中使用正则表达式无法匹配Srt字幕_Java_Regex - Fatal编程技术网

在Java中使用正则表达式无法匹配Srt字幕

在Java中使用正则表达式无法匹配Srt字幕,java,regex,Java,Regex,在本代码中,请尝试解析srt字幕: public class MatchArray { public static void main(String args[]) { File file = new File( "C:/Users/Thiago/workspace/SubRegex/src/Dirty Harry VOST - Clint Eastwood.srt"); { try { Scanner in

在本代码中,请尝试解析srt字幕:

public class MatchArray {

public static void main(String args[]) {

    File file = new File(
            "C:/Users/Thiago/workspace/SubRegex/src/Dirty Harry VOST - Clint Eastwood.srt");
    {

        try {
            Scanner in = new Scanner(file);

            try {
                String contents = in.nextLine();

                while (in.hasNextLine()) {
                    contents = contents + "\n" + in.nextLine();
                }



                String pattern = "([\\d]+)\r([\\d]{2}:[\\d]{2}:[\\d]{2}),([\\d]{3})[\\s]*-->[\\s]*([\\d]{2}:[\\d]{2}:[\\d]{2}),([\\d]{3})\r(([^|\r]+(\r|$))+)";


                Pattern r = Pattern.compile(pattern);

                // Now create matcher object.
                Matcher m = r.matcher(contents);

                ArrayList<String> start = new ArrayList<String>();
                while (m.find()) {
                    start.add(m.group(1));
                    start.add(m.group(2));
                    start.add(m.group(3));
                    start.add(m.group(4));
                    start.add(m.group(5));
                    start.add(m.group(6));
                    start.add(m.group(7));


                    System.out.println(start);

                }
            }

            finally {
                in.close();

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

它起作用了。那么,我如何让它捕捉整个字幕呢?

我不太理解您的需要,但我认为这会有所帮助。 请尝试使用正则表达式:

(\\d+?)\\s*(\\d+?:\\d+?:\\d+?,\\d+?)\\s+-->\\s+(\\d+?:\\d+?:\\d+?,\\d+?)\\s+(.+)
我试了一下,效果很好


我希望这能有所帮助。

可能重复的,请查看第二个答案以获得正确的正则表达式。谢谢@Rossiar,我已经试过了,但我认为它有太多的组,而且这个会更快,如果我能让它工作。你能发布一个示例输入行和一个捕获组的示例吗?输入行:1 00:05:29384-->00:05:30974耶稣!2 00:05:31422-->00:05:33376前往旧金山市。你所说的“俘虏集团”是什么意思?我只需要将id、开始时间、结束时间和文本分为不同的组。
(\\d+?)\\s*(\\d+?:\\d+?:\\d+?,\\d+?)\\s+-->\\s+(\\d+?:\\d+?:\\d+?,\\d+?)\\s+(.+)