我的要解析的字符串是一个http响应,其中竖条的分隔' |'冒号':' 和逗号','
"1701|919422522891:224c1214-bb95-414d-ba76-77db95370545,1701|918275004333:5e93a439-2644-4455-9f01-f27e6cf0cde6"
我尝试使用以下代码进行解析
public void split2(){
String input = "1701|919422522891:224c1214-bb95-414d-ba76-77db95370545," +
"1701|918275004333:5e93a439-2644-4455-9f01-f27e6cf0cde6";
Matcher matcher = Pattern.compile("\\|.*?\\:\\,").matcher(input);
int nr = 0;
while (matcher.find()) {
System.out.println(matcher.group());
}
}
更新 - 工作代码
拆分方法效果很好
public String[] split(String regex,String input)
{
input = "1701|919422522891:224c1214-bb95-414d-ba76-77db95370545," +
"1701|918275004333:5e93a439-2644-4455-9f01-f27e6cf0cde6";
regex = "\\||:|," ;
String[] soso = Pattern.compile(regex).split(input, input.length());
for(String s :soso){
Log.e("",s.toString());
}
return null;
}
答案 0 :(得分:3)
您可以使用:
Matcher matcher = Pattern.compile("[^|]*\\|[^:]*:[^,]*,").matcher(input);
说明:此正则表达式"[^|]*\\|[^:]*:[^,]*,"
表示:
[^|]*
- 在管道前匹配0个或多个字符\\|
- 匹配管道[^:]*
- 在冒号前匹配0个或多个字符:
- 匹配冒号[^,]*
- 匹配逗号前的0个或多个字符,
- 匹配昏迷答案 1 :(得分:0)