Why does the following code return the output below?
I would expect that 2 and 3 provide the same string splitting of 1.
Log.d(TAG, " 1 ---------------------------");
String originalText = "hello. .hello1";
Pattern p = Pattern.compile("[a-zA-Z]+|\\s|\\W|\\d");
Matcher m = p.matcher(originalText);
while (m.find()) {
Log.d(TAG, m.group(0));
}
Log.d(TAG, "2 --------------------------- " + originalText);
String [] scrollString = p.split(originalText);
int i;
for (i=0; i<scrollString.length; i++)
Log.d(TAG, scrollString[i]);
Log.d(TAG, "3 --------------------------- " + originalText);
scrollString = originalText.split("[a-zA-Z]+|\\s|\\W|\\d");
for (i=0; i<scrollString.length; i++)
Log.d(TAG, scrollString[i]);
OUTPUT:
1 ---------------------------
hello
.
.
hello
1
2 ---------------------------
3 ---------------------------
No. 1 will find the pattern and return that, whereas No. 2 and 3 will return the text in between the found pattern (which serves as the delimiter in those cases).