From c1a78e8724a5b4296bb794a5e6d5a15129b008e7 Mon Sep 17 00:00:00 2001 From: Massimiliano Assante Date: Wed, 18 Sep 2019 12:46:46 +0200 Subject: [PATCH] added method Set getMentionedUsernames --- .../socialutillibrary/Utils.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/main/java/org/gcube/social_networking/socialutillibrary/Utils.java b/src/main/java/org/gcube/social_networking/socialutillibrary/Utils.java index de99591..80ad189 100644 --- a/src/main/java/org/gcube/social_networking/socialutillibrary/Utils.java +++ b/src/main/java/org/gcube/social_networking/socialutillibrary/Utils.java @@ -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("shared ").append("a file.").append(" ").toString(); return sb.toString(); } + /** + * + * @param postText + * @return a set containing the list of mentions found + */ + public static Set getMentionedUsernames(String postText) { + Set 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; + } /** *