拆分多个分隔符

时间:2016-07-22 08:14:35

标签: java regex

我有一个字符串,其中包含由|分隔的网址像这样:

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

但是在某些网址中我可能有caracheter | ,所以我可以将其拆分为.split(" | http://")但问题是在里面一些网址包含这样的其他网址

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

那么如何使用正则表达式分割|http:// or =http:// or =http%25253A%25252F%25252F

2 个答案:

答案 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

答案 1 :(得分: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...