2016-07-22 172 views
-4

我有一個字符串,它包含由|像這樣:拆分多個分隔符

http://...|http://...|http://...

但某些URL裏面我可以有caracheter |,所以我可以.split分割它(「| HTTP://」),但問題是,某些URL裏面包含了另一個網址,這樣

http://...|http://..=http://...=http://...|http://...=http%25253A%25252F%25252F...

我那麼,怎樣才能使用正則表達式拆分通過|http:// or =http:// or =http%25253A%25252F%25252F

+2

分享你的研究可以幫助大家。告訴我們你試過的東西以及爲什麼 它不符合你的需求。這表明你已經花時間 試圖幫助自己,它使我們避免重申明顯的答案, ,最重要的是它可以幫助您獲得更具體和相關的答案! 另請參閱[如何問](http://stackoverflow.com/questions/how-to-ask) –

+0

對我的答案的任何反饋? –

回答

2

您可以使用下面的代碼:

String str = "http://www.google.com|https://support.microsoft.com/en-us/kb/301982|http://www.tutorialspoint.com/java/lang/string_split.htm"; 
String delimiters = "\\|(?=http)"; 

// analyzing the string 
String[] urls = str.split(delimiters); 

// prints the number of tokens 
System.out.println("Count of urls= " + urls.length); 

for(String url: urls) { 
    System.out.println(url); 
} 

它會分裂的|其次http。此示例的輸出是:

Count of urls = 3 
http://www.google.com 
https://support.microsoft.com/en-us/kb/301982 
http://www.tutorialspoint.com/java/lang/string_split.htm 
0

你可以試試下面的代碼:

// As your question in this string contains three https 
String httpStr = "http://...|http://..=http://...=http://...|http://...=http%25253A%25252F%25252F..."; 
// Split the string with 'http' that preceded by | 
String[] https = httpStr.split("(?<=\\|)http"); 
for (String http : https) { 
    System.out.println("http = http" + http); 
} 

而且它的結果:

http = http://...| 
http = http://..=http://...=http://...| 
http = http://...=http%25253A%25252F%25252F...