Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.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 打印出正则表达式的最后一个匹配项_Java_Regex - Fatal编程技术网

Java 打印出正则表达式的最后一个匹配项

Java 打印出正则表达式的最后一个匹配项,java,regex,Java,Regex,我有以下代码: String responseData = "http://xxxxx-f.frehd.net/i/world/open/20150426/1370235-005A/EPISOD-1370235-005A-016f1729028090bf_,892,144,252,360,540,1584,2700,.mp4.csmil/.m3u8"; "http://xxxxx-f.frehd.net/i/world/open/20150426/1370235-005A/EPISOD-1370

我有以下代码:

String responseData = "http://xxxxx-f.frehd.net/i/world/open/20150426/1370235-005A/EPISOD-1370235-005A-016f1729028090bf_,892,144,252,360,540,1584,2700,.mp4.csmil/.m3u8";
"http://xxxxx-f.frehd.net/i/world/open/20150426/1370235-005A/EPISOD-1370235-005A-016f1729028090bf_,892,144,252,360,540,1584,2700,.mp4.csmil/.m3u8";

String pattern = ^(https://.*\.54325)$;

Pattern pr = Pattern.compile(pattern);  

            Matcher math = pr.matcher(responseData);


            if (math.find()) {

            // print the url 

            }
            else {
                System.out.println("No Math");
            }
我想打印出最后一个字符串,该字符串以http开头,以.m3u8结尾。我该怎么做?我卡住了。感谢所有的帮助


我现在遇到的问题是,当我找到一个数学和打印字符串的内容时,我从responseData获得了所有信息。

如果您需要在末尾获得一些类似的子字符串,您需要确保正则表达式引擎在进行所需匹配之前已经使用了尽可能多的字符

另外,模式中有一个
^
,表示字符串的开头。因此,它从一开始就开始匹配

只需使用
lastIndexOf
子字符串
,就可以实现您想要的:

System.out.println(str.substring(str.lastIndexOf("http://")));
或者,如果需要正则表达式,则需要使用

String pattern = ".*(http://.*?\\.m3u8)$";
并使用
math.group(1)
打印值

:

输出:

http://xxxxx-f.akamaihd.net/i/world/open/20150426/1370235-005A/EPISOD-1370235-005A-016f1729028090bf_,892,144,252,360,540,1584,2700,.mp4.csmil/index_6_av.m3u8

如果我在我的responseData中添加更多url,这是否有效?因为反应是动态的。我想打印出最后一个url,不管我有多少个。数学怎么样;与math.group()不同;?此正则表达式将始终匹配整个字符串(
group()
),并且最后一个URL将始终位于
group(1)
中,因为我使用编号的捕获组捕获它。您只需
lastIndexOf
即可获取最后一个URL,请检查我的更新。以及要测试的程序:
http://xxxxx-f.akamaihd.net/i/world/open/20150426/1370235-005A/EPISOD-1370235-005A-016f1729028090bf_,892,144,252,360,540,1584,2700,.mp4.csmil/index_6_av.m3u8