aboutsummaryrefslogtreecommitdiff
path: root/juick-common/src/main/java/com/juick/util
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-09-29 16:39:37 +0300
committerGravatar Vitaly Takmazov2018-09-29 16:39:37 +0300
commit1621fb481fe91b084a48326419969f3a22ccbbe2 (patch)
treebe6aa4acd21ee5d8e7edf8cde95cffe64590ec0e /juick-common/src/main/java/com/juick/util
parent80f305e6f5d22abc38807fd6fad37b5cb450c249 (diff)
Mentions
Diffstat (limited to 'juick-common/src/main/java/com/juick/util')
-rw-r--r--juick-common/src/main/java/com/juick/util/MessageUtils.java13
1 files changed, 12 insertions, 1 deletions
diff --git a/juick-common/src/main/java/com/juick/util/MessageUtils.java b/juick-common/src/main/java/com/juick/util/MessageUtils.java
index bac24507..4de68cb9 100644
--- a/juick-common/src/main/java/com/juick/util/MessageUtils.java
+++ b/juick-common/src/main/java/com/juick/util/MessageUtils.java
@@ -68,6 +68,9 @@ public class MessageUtils {
private final static String replyNumberRegex = "((?<=\\s)|(?<=\\A))\\/(\\d+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))";
+ private final static String usernameRegex = "((?<=\\s)|(?<=\\A))@([\\w\\-]{2,16})((?=\\s)|(?=\\Z)|(?=\\p{Punct}))";
+ private final static Pattern usernamePattern = Pattern.compile(usernameRegex);
+
public static String formatMessageCode(String msg) {
msg = msg.replaceAll("&", "&amp;");
msg = msg.replaceAll("<", "&lt;");
@@ -139,7 +142,7 @@ public class MessageUtils {
// @username
// <a href="http://juick.com/username/">@username</a>
- msg = msg.replaceAll("((?<=\\s)|(?<=\\A))@([\\w\\-]{2,16})((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1<a href=\"https://juick.com/$2/\">@$2</a>$3");
+ msg = msg.replaceAll(usernameRegex, "$1<a href=\"https://juick.com/$2/\">@$2</a>$3");
// (http://juick.com/last?page=2)
// (<a href="http://juick.com/last?page=2" rel="nofollow">juick.com</a>)
@@ -301,4 +304,12 @@ public class MessageUtils {
}
return input;
}
+ public static List<String> getMentions(Message msg) {
+ Matcher usernameMatcher = usernamePattern.matcher(msg.getText());
+ List<String> result = new ArrayList<>();
+ while (usernameMatcher.find()) {
+ result.add(usernameMatcher.group());
+ }
+ return result;
+ }
}