| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Text.RegularExpressions; |
| | 5 | |
|
| | 6 | | namespace DCL.Social.Chat.Mentions |
| | 7 | | { |
| | 8 | | public static class MentionsUtils |
| | 9 | | { |
| | 10 | | private const string MENTION_PATTERN = @"\B@[a-zA-Z\d]{3,15}(#[a-zA-Z\d]{4})?"; |
| 1 | 11 | | private static readonly Regex MENTION_REGEX = new (MENTION_PATTERN); |
| | 12 | |
|
| | 13 | | public static bool IsAMention(string text) |
| | 14 | | { |
| 0 | 15 | | var match = MENTION_REGEX.Match(text); |
| 0 | 16 | | return match.Success; |
| | 17 | | } |
| | 18 | |
|
| | 19 | | public static bool IsUserMentionedInText(string userName, string text) => |
| 24 | 20 | | text.Contains($"@{userName}", StringComparison.OrdinalIgnoreCase); |
| | 21 | |
|
| | 22 | | public static bool TextContainsMention(string text) |
| | 23 | | { |
| 0 | 24 | | Match match = MENTION_REGEX.Match(text); |
| 0 | 25 | | return match.Success; |
| | 26 | | } |
| | 27 | |
|
| | 28 | | public static IEnumerable<string> GetAllMentions(string input) |
| | 29 | | { |
| 16 | 30 | | MatchCollection matches = MENTION_REGEX.Matches(input); |
| 16 | 31 | | return matches.Select(match => match.Value); |
| | 32 | | } |
| | 33 | |
|
| | 34 | | public static string ReplaceMentionPattern(string input, Func<string, string> replacementCallback) |
| | 35 | | { |
| 0 | 36 | | return MENTION_REGEX.Replace(input, match => replacementCallback?.Invoke(match.Value)); |
| | 37 | | } |
| | 38 | | } |
| | 39 | | } |