in java/src/main/java/com/twitter/twittertext/Extractor.java [236:273]
public List<Entity> extractMentionsOrListsWithIndices(String text) {
if (isEmptyString(text)) {
return Collections.emptyList();
}
// Performance optimization.
// If text doesn't contain @/@ at all, the text doesn't
// contain @mention. So we can simply return an empty list.
boolean found = false;
for (char c : text.toCharArray()) {
if (c == '@' || c == '@') {
found = true;
break;
}
}
if (!found) {
return Collections.emptyList();
}
List<Entity> extracted = new ArrayList<Entity>();
Matcher matcher = Regex.VALID_MENTION_OR_LIST.matcher(text);
while (matcher.find()) {
String after = text.substring(matcher.end());
if (!Regex.INVALID_MENTION_MATCH_END.matcher(after).find()) {
if (matcher.group(Regex.VALID_MENTION_OR_LIST_GROUP_LIST) == null) {
extracted.add(new Entity(matcher, Entity.Type.MENTION,
Regex.VALID_MENTION_OR_LIST_GROUP_USERNAME));
} else {
extracted.add(new Entity(matcher.start(Regex.VALID_MENTION_OR_LIST_GROUP_USERNAME) - 1,
matcher.end(Regex.VALID_MENTION_OR_LIST_GROUP_LIST),
matcher.group(Regex.VALID_MENTION_OR_LIST_GROUP_USERNAME),
matcher.group(Regex.VALID_MENTION_OR_LIST_GROUP_LIST),
Entity.Type.MENTION));
}
}
}
return extracted;
}