Skip to content

Instantly share code, notes, and snippets.

@highel
Created January 5, 2018 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save highel/bd2a5b15e08712f1171c565f7cf141c0 to your computer and use it in GitHub Desktop.
Save highel/bd2a5b15e08712f1171c565f7cf141c0 to your computer and use it in GitHub Desktop.
split text with delimeters
public class Split {
public static Pattern whitespace = Pattern.compile("\\s+");
public static void main(String[] s) {
List<Block> blocks = splitWithDelimiters("r2ejh2j 24 3");
blocks = splitWithDelimiters("СЛОН ТОПТПАИТ 24 3343");
blocks = splitWithDelimiters(" топтает ");
blocks = splitWithDelimiters(" тооп");
blocks = splitWithDelimiters(" ");
blocks = splitWithDelimiters("");
blocks = splitWithDelimiters("NODELIMETER");
}
String obfuscate(String data) {
return data;
}
static List<Block> splitWithDelimiters(String s) {
if (s == null || s.isEmpty())
return Collections.emptyList();
LinkedList<Block> blocks = new LinkedList<Block>();
Matcher matcher = whitespace.matcher(s);
boolean found;
do {
found = matcher.find();
if (found) {
blocks.add(new Block(matcher.start(), matcher.end(),
s.substring(matcher.start(), matcher.end()),
true));
}
} while (found);
int lastMatched = -1;
int from;
int to;
ListIterator<Block> it = blocks.listIterator();
while (it.hasNext()) {
Block next = it.next();
if (next.whiteSpace) {
from = lastMatched + 1;
to = next.from;
lastMatched = next.to - 1;
if (to - from > 0) {
it.previous();
it.add(new Block(from, to, s.substring(from, to), false));
}
}
}
//EOL
from = lastMatched + 1;
to = s.length();
if (to - from > 0) {
it.add(new Block(from, to,
s.substring(from, to), false));
}
return blocks;
}
static class Block {
final int from;
final int to;
final String text;
final boolean whiteSpace;
public Block(int from, int to, String text, boolean whiteSpace) {
this.from = from;
this.to = to;
this.text = text;
this.whiteSpace = whiteSpace;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment