I need to know how mention works, how should find mentions during a text. are we must to find first of '@' and last of not @"^[a-zA-Z0-9_,]+$"
thank you for sharing your experience
string comment=" hi @fri.tara3^";
mention is : "@fri.tara3"
I need to know how mention works, how should find mentions during a text. are we must to find first of '@' and last of not @"^[a-zA-Z0-9_,]+$"
thank you for sharing your experience
string comment=" hi @fri.tara3^";
mention is : "@fri.tara3"
Looks like a good fit for regular expressions. There are multiple ways to solve this.
Here's the simplest one:
(?<mention>@[a-zA-Z0-9_.]+)[^a-zA-Z0-9_.]
[^ ... ]
does the negation bit(?<mention> ... )
declares an explictit group to capture mention without including the non-matching character immediately following the mention.A cleaner pattern would use a feature called look-ahead:
@[a-zA-Z0-9_.]+?(?![a-zA-Z0-9_.])
+?
. This ensures that matched mention is as short as possible.Lookaheads are a tad less known and may become a pain to read if pattern grows too long. But it is a useful tool to know.
Full example using C#:
string comment = "hi @fri.tara3^ @hjh not a mention @someone";
const String pattern = "@[a-zA-Z0-9_.]+?(?![a-zA-Z0-9_.])";
var matches = Regex.Matches(comment, pattern);
for (int i = 0; i < matches.Count; i++)
{
Console.WriteLine(matches[i].Value);
}