使用Pattern.compile拆分文本

时间:2013-10-13 03:49:27

标签: regex metacharacters

以下是文字行:

003 STATE BANK OF BIK & JAI A/C.1 2 1,01,500.00 1 3,160.00 98,340.00+

以下是我要拆分的代码段:

Pattern pat = Pattern.compile("[ ]");
String strs[] = pat.split(s);
for (int i = 0; i < strs.length; i++) {
    System.out.println("Next Token = " + strs[i]);
}

这是我得到的:

003,STATE,BANK,OF,BIK,*,JAI...etc.

我真正想要的是:

003,STATE BANK OF BIK & JAI,A/C.1,2.1,01,500.00...etc

我使用哪种模式或元字符来完成此任务?

1 个答案:

答案 0 :(得分:0)

对于您的情况,此拆分调用将起作用:

String data = "003 STATE BANK OF BIK & JAI A/C.1 2 1,01,500.00 1 3,160.00 98,340.00+";
String[] arr = data.split(" +(?=\\S*\\d)|(?<![A-Z&]) +");
System.out.println(Arrays.toString(arr));

输出:

[003, STATE BANK OF BIK & JAI, A/C.1, 2, 1,01,500.00, 1, 3,160.00, 98,340.00+]