Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.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_String_Extract - Fatal编程技术网

从Java中的另一个字符串中提取字符串

从Java中的另一个字符串中提取字符串,java,string,extract,Java,String,Extract,下面是一个字符串: "http://l2.yimg.com/bt/api/res/1.2/iis49xBsStLiYI6LjauR6Q--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/http://media.zenfs.com/fr_FR/News/LeMonde.fr/1515504_3_f73c_le-cyber-harcelement-est-une-realite-trop-lo_450282425a88c544c2ff4121a5

下面是一个字符串:

"http://l2.yimg.com/bt/api/res/1.2/iis49xBsStLiYI6LjauR6Q--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/http://media.zenfs.com/fr_FR/News/LeMonde.fr/1515504_3_f73c_le-cyber-harcelement-est-une-realite-trop-lo_450282425a88c544c2ff4121a5d9dab4.jpg"
此字符串是两个URL的串联。我只想提取第二个URL:

"http://media.zenfs.com/fr_FR/News/LeMonde.fr/1515504_3_f73c_le-cyber-harcelement-est-une-realite-trop-lo_450282425a88c544c2ff4121a5d9dab4.jpg"
如何使用Java实现这一点?

删除在开始时未找到的“http://”之前的所有内容:

String url2 = str.replaceAll("(?i).+(?=https?://)", "");
这将不敏感地工作,并匹配
http
https
协议。

尝试使用string的方法,如下所示:

String oneURL = twoURLs.split("(?<!^)(?=http://)")[1];
["http://l2.yimg.com/bt/api/res/1.2/iis49xBsStLiYI6LjauR6Q--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/", "http://media.zenfs.com/fr_FR/News/LeMonde.fr/1515504_3_f73c_le-cyber-harcelement-est-une-realite-trop-lo_450282425a88c544c2ff4121a5d9dab4.jpg"]
[1]
仅获取该数组的第二个元素

这里是正则表达式的解释和演示:

试试这个。“str”是url字符串

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

如果要提取URL,只需找到
http
的最后一个实例,并获取子字符串:

String secondUrl = firstUrl.substring(firstUrl.lastIndexOf("http"));

一个简单的
str.substring(str.indexOf(“http://”,1))
就足够了。我喜欢
str.substring(108)
。它完全符合海报的要求。+1用于处理
https
,尽管我几乎不认为不区分大小写是必要的。谢谢,但我在atm上工作-几个小时前就结束了:)+1;我不知道您可以在java中使用
(?)
。@RyanCarlson afaik
(?ims)
。默认情况下,global是打开的。它没有在任何地方显示,但我确实发现了另一件有趣的事情:在regex
(abc((?I)xyz)
xyz
不区分大小写,但
abc
不区分大小写。