I try to get string between , here is my implementation:
String str = "ZZZZL AFFF ";
Pattern pattern = Pattern.compile("");
String[] result = pattern.split(str);
System.out.println(Arrays.toString(result));
it return
[ZZZZL , AFFF ]
But my expectation is:
[ dsn , AFG ]
Where am i wrong and how to correct it ?
解决方案
Your pattern is fine. But you shouldn't be split()ting it away, you should find() it. Following code gives the output you are looking for:
String str = "ZZZZL AFFF ";
Pattern pattern = Pattern.compile("");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(1));
}