正则表达式基于Path提取域名

时间:2015-03-27 13:30:48

标签: java regex

我需要一个正则表达式来根据路径从json下面提取服务器名称。 理想情况下,我应该得到server1& server2& https之间的字符串/upload/image/app { "url1" : "https://server1/upload/image/app/test1.jpg", "video" : "https://www.youtube.com/watch?v=K7QaD3l1yQA", "type" : "youtube", "url2" : "https://server2/upload/image/app/test2.jpg" } 作为输出并忽略youtube网址。

https://(.*?)/upload/image/app

试过这个,但我知道这不起作用:

{{1}}

2 个答案:

答案 0 :(得分:1)

^(?:http|https)\:\/\/(.*?)\/(?:.*?)$

这应该可以解决问题。例子:

<?php
preg_match("/^(?:http|https)\:\/\/(.*?)\/(?:.*?)$/", "https://server1/upload/image/app/test1.jpg", $matches);
echo $matches[1]; //server1
?>

使用正则表达式并不困难,我建议您至少开始学习基础知识,因为它们可能很有用

答案 1 :(得分:0)

你可以试试这样的东西:

^.*http.*//(\S+)/upload/ima.*

编辑,包括JAVA示例:

Pattern p = Pattern.compile("^.*http.*//(\\S+)/upload/ima.*");
Matcher m = p.matcher(nextLine);

if (m.find()) {
    System.out.println(m.group(1));
}

检查:Using Regular Expressions to Extract a Value in Java