Let's say I have two Java Patterns, one for finding whitespace at the beginning of the line, and the other for finding non-whitespace at the beginning of the line:
Pattern ws = Pattern.compile("^\\s+");
Pattern nws = Pattern.compile("^\\S+");
String text = "\tSome \n\t text \n that needs \t parsing.";
I want to loop through the text, separating it blocks of whitespace and blocks of non-whitespace, removing each token from the beginning of text:
while(text.length() > 0) {
String nextToken = "";
try {
//TODO: detect grouping and move it to nextToken.
} catch (Exception e) {
//TODO: error handling
}
if(nextToken.length() > 0)
_tokens.add(nextToken);
}
I don't just want to replace stuff. "\tSome \n\t text \n that needs \t parsing." should split to ["\t", "Some", "\n\t ", "text", ...]
How would you accomplish something like this?