added method Set<String> getMentionedUsernames

This commit is contained in:
Massimiliano Assante 2019-09-18 12:46:46 +02:00
parent b8e1f20d97
commit c1a78e8724
1 changed files with 18 additions and 0 deletions

View File

@ -3,7 +3,9 @@ package org.gcube.social_networking.socialutillibrary;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -41,6 +43,8 @@ public class Utils {
*/
private static final String HASHTAG_REGEX = "^#\\w+([-_.]?\\w+)*|\\s#\\w+([-_.]?\\w+)*|(?<=[\\[({])#\\w+([-_.]?\\w+)";
private static final String MENTIONS_REGEX = "^@\\w+([-_.]?\\w+)*|\\s@\\w+([-_.]?\\w+)*|(?<=[\\[({])@\\w+([-_.]?\\w+)";
/**
* IPv4 regex
@ -80,6 +84,20 @@ public class Utils {
sb.append("<span style=\"color:gray; font-size:12px;\">shared </span><a class=\"link\" href=\"").append(url).append("\" target=\"_blank\">").append("a file.").append("</a> ").toString();
return sb.toString();
}
/**
*
* @param postText
* @return a set containing the list of mentions found
*/
public static Set<String> getMentionedUsernames(String postText) {
Set<String> mentions = new HashSet<>();
Pattern MY_PATTERN = Pattern.compile(MENTIONS_REGEX);
Matcher matcher = MY_PATTERN.matcher(postText);
while (matcher.find()) {
mentions.add(matcher.group());
}
return mentions;
}
/**
*