2013-10-08 79 views
1

我有這樣的完整鏈接:如何用正則表達式去掉部分URL?

http://localhost:8080/suffix/rest/of/link 

如何用Java編寫的正則表達式將返回後綴的網址只主要部分:http://localhost/suffix而不:/rest/of/link

  • 可能的協議:HTTP,HTTPS
  • 可能的端口:許多可能性

我認爲我需要'/'馬克的第三發生(含)後刪除全部文本。 我想這樣做,但我不知道正則表達式,請問如何正確寫入正則表達式?

String appUrl = fullRequestUrl.replaceAll("(.*\\/{2})", ""); //this removes 'http://' but this is not my case 
+1

什麼是正則表達式的一點:

import java.net.*; import java.io.*; public class ParseURL { public static void main(String[] args) throws Exception { URL aURL = new URL("http://example.com:80/docs/books/tutorial" + "/index.html?name=networking#DOWNLOADING"); System.out.println("protocol = " + aURL.getProtocol()); System.out.println("authority = " + aURL.getAuthority()); System.out.println("host = " + aURL.getHost()); System.out.println("port = " + aURL.getPort()); System.out.println("path = " + aURL.getPath()); System.out.println("query = " + aURL.getQuery()); System.out.println("filename = " + aURL.getFile()); System.out.println("ref = " + aURL.getRef()); } } 

這裏是由程序顯示的輸出?找到第四個'/'的索引。 –

+2

使用一個'URL'對象。 –

+0

關鍵是從URL中檢索基本應用程序url(protocol + serverName + serverPort + contextPath),它可以是完整的,也就是說,它也可以具有我不感興趣的servlet路徑和參數。 – Roman

回答

1

的代碼獲得URL的主要部分:

import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public class RegexpExample { 
    public static void main(String[] args) { 
     String urlStr = "http://localhost:8080/suffix/rest/of/link"; 
     Pattern pattern = Pattern.compile("^((.*:)//([a-z0-9\\-.]+)(|:[0-9]+)/([a-z]+))/(.*)$"); 

     Matcher matcher = pattern.matcher(urlStr); 
     if(matcher.find()) 
     { 
      //there is a main part of url with suffix: 
      String mainPartOfUrlWithSuffix = matcher.group(1); 
      System.out.println(mainPartOfUrlWithSuffix); 
     } 
    } 
} 
1

我不知道爲什麼要使用正則表達式這一點。 Java爲您提供了相同的功能,可以提供一個Query URL Objects

下面是來自同一site採取一個例子來說明它是如何工作的:

protocol = http 
authority = example.com:80 
host = example.com 
port = 80 
path = /docs/books/tutorial/index.html 
query = name=networking 
filename = /docs/books/tutorial/index.html?name=networking 
ref = DOWNLOADING 
+0

@Boris蜘蛛: - 你提到這個嗎? –

+0

不錯,但重點是,我需要分離contextPath,這是在URL中的端口後的第一部分。所以'URL'類無法識別它(但它會很好)。在你的例子中,contextPath是'docs' - 並且使用'URL',我仍然需要解析'path'並從'path'中排除其餘文本。 – Roman

+0

如何找到/的索引然後做你想做的事? –