[英]Java Regex - multiple urls match in one output string
I have this string:我有这个字符串:
jwplayer("vplayer").setup({sources:[{file:"https://v5.vidhd.net/kmxsus4zpjumwmesrlwey575ndnsv4xwfgemw6pmtbfbdks3bqofp5s6incq/v.mp4",label:"720p"},{file:"https://v5.vidhd.net/kmxsus4zpjumwmesrlwey575ndnsv4xwfgemw6pmt2xbbks3bqofaojtzsgq/v.mp4",label:"360p"}],image:"https://v5.vidhd.net/i/01/00007/vdachffyt692.jpg"
I want to get only the two urls ie the ones after file:
.我只想获取两个网址,即
file:
之后的网址。 The desired output string
should be like:所需的 output
string
应如下所示:
https://v5.vidhd.net/kmxsus4zpjumwmesrlwey575ndnsv4xwfgemw6pmtbfbdks3bqofp5s6incq/v.mp4, https://v5.vidhd.net/kmxsus4zpjumwmesrlwey575ndnsv4xwfgemw6pmt2xbbks3bqofaojtzsgq/v.mp4
How can I get this in java?我怎样才能在 java 中得到这个?
"file:\"(.*?)\""
, but I got only the first url even that I've put matcher.find()
inside a while loop."file:\"(.*?)\""
,但我只得到了第一个 url ,即使我已经将matcher.find()
放在了一个 while 循环中。 Edit: This regex works fine also the one in the @azro answer.编辑:这个正则表达式也适用于@azro 答案中的那个。 My problem was that some code inside the while loop throws an error and stops the loop after one iteration (thanks to @pafau_k).
我的问题是 while 循环中的某些代码会在一次迭代后引发错误并停止循环(感谢@pafau_k)。 Sorry for the inconvenience..
带来不便敬请谅解..
To get the urls a simple regex like this file:"(https.*?)" would work, the *?
要获取网址,可以使用像以下文件这样的简单正则表达式:"(https.*?)",
*?
means a few as possible, to stop at first quote after意味着尽可能少,在第一次引用之后停止
String content = "jwplayer(\"vplayer\").setup({sources:[{file:\"https://v5.vidhd.net/kmxsus4zpjumwmesrlwey575ndnsv4xwfgemw6pmtbfbdks3bqofp5s6incq/v.mp4\",label:\"720p\"},{file:\"https://v5.vidhd.net/kmxsus4zpjumwmesrlwey575ndnsv4xwfgemw6pmt2xbbks3bqofaojtzsgq/v.mp4\",label:\"360p\"}],image:\"https://v5.vidhd.net/i/01/00007/vdachffyt692.jpg\"";
Matcher m = Pattern.compile("file:\"(http.*?)\"").matcher(content);
while(m.find())
System.out.println(m.group(1));
Stream version to collect at same time Stream版本同时收集
String res = Pattern.compile("file:\"(http.*?)\"")
.matcher(content)
.results()
.map(r -> r.group(1))
.collect(Collectors.joining(" "));
System.out.println(res); // https://v5.vidhd.net/kmxsus4zpjumwmesrlwey575ndnsv4xwfgemw6pmtbfbdks3bqofp5s6incq/v.mp4 https://v5.vidhd.net/kmxsus4zpjumwmesrlwey575ndnsv4xwfgemw6pmt2xbbks3bqofaojtzsgq/v.mp4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.