用方括号拆分字符串

时间:2016-02-28 22:36:52

标签: java regex string

我有一个字符串,如下所示:1。[Aagaard,Lindsay] [SeniorPolicyAdvisor] [TREASURYBOARDSECRETARIAT] [DEPUTYPREMIERANDPRESIDENTOFTHETREASURYBOARD,多伦多] [416-327-0948] [lindsay.aagaard@ontario.ca] 2. [阿尔托,霭] [ProbationOfficer] [CHILDRENANDYOUTHSERVICES] [桑德贝,桑德贝] [807-475-1310] [margaret.aalto@ontario.ca]

我想把它分成这样一个arraylist:

1

AAGAARD,林赛

SeniorPolicyAdvisor

有什么建议吗?

3 个答案:

答案 0 :(得分:1)

我读了JavaDoc并使用了Pattern和Matcher,如下所示:

Pattern p = Pattern.compile("\\[(.*?)\\]");
Matcher m = p.matcher(tableContent);

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

答案 1 :(得分:1)

首先删除第一个和最后一个括号,然后按']分拆[':

String arr = "[Aalto,Margaret][ProbationOfficer][CHILDRENANDYOUTHSERVICES]";
String[] items = arr.substring(1, arr.length() - 1).split("][");

答案 2 :(得分:0)

简单地说:

String[] list = str.split("\\[");

for(int i = 0 ; i < list.length ; i++) {
    list[i] = list[i].replace("\\]", "");
}