2

I have this code:

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"); } 

I want to print out the last string that starts with http and ends with .m3u8. How do I do this? I'm stuck. All help is appreciated.

The problem I have now is that when I find a math and what to print out the string, I get everything from responseData.

1 Answer 1

1

In case you need to get some substring at the end that is preceded by similar substrings, you need to make sure the regex engine has already consumed as many characters before your required match as possible.

Also, you have a ^ in your pattern that means beginning of a string. Thus, it starts matching from the very beginning.

You can achieve what you want with just lastIndexOf and substring:

System.out.println(str.substring(str.lastIndexOf("http://"))); 

Or, if you need a regex, you'll need to use

String pattern = ".*(http://.*?\\.m3u8)$"; 

and use math.group(1) to print the value.

Sample code:

import java.util.regex.*; public class HelloWorld{ public static void main(String []args){ String str = "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_0_av.m3u8" + "EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2795000,RESOLUTION=1280x720,CODECS=avc1.64001f, mp4a.40.2" + "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"; String rx = ".*(http://.*?\\.m3u8)$"; Pattern ptrn = Pattern.compile(rx); Matcher m = ptrn.matcher(str); while (m.find()) { System.out.println(m.group(1)); } } } 

Output:

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 

Also tested on RegexPlanet

Sign up to request clarification or add additional context in comments.

3 Comments

Will this work if I add more url to my responseData? Because the response is dynamic. I want to print out the last url regardless of how many I have. And how is math.group (1); different from math.group();?
This regex will always match the whole string (group()), and the last URL will always be in the group(1) as I am capturing it with a numbered capture group.
You can get the last URL with a mere lastIndexOf, please check my update. And also the program to test: tutorialspoint.com/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.