Skip to content

Instantly share code, notes, and snippets.

@bobaikato
Created May 11, 2022 17:19
Show Gist options
  • Save bobaikato/8e7297794fd4d82a68397837a691392f to your computer and use it in GitHub Desktop.
Save bobaikato/8e7297794fd4d82a68397837a691392f to your computer and use it in GitHub Desktop.
Extract HashTags from Text
public static @NotNull List<String> extractHashtags(final @NotNull String text) {
final var hashTags = new ArrayList<String>();
Arrays.stream(text.split("\\s"))
.filter(word -> word.startsWith(HASH))
.distinct()
.parallel()
.forEachOrdered(
word -> {
if (word.matches("[#A-Za-z0-9]+") && word.length() > 1) {
hashTags.add(word);
} else {
final var wordChar = word.toCharArray();
final var wordBuilder = new StringBuilder().append(wordChar[0]);
for (int i = 1; i < wordChar.length; ++i) {
if (Character.isAlphabetic(wordChar[i]) || Character.isDigit(wordChar[i])) {
wordBuilder.append(wordChar[i]);
} else {
hashTags.add(wordBuilder.toString());
break;
}
}
}
});
return hashTags;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment