Imports System.Collections.Generic
Dim input As String = String.Join(Environment.NewLine, { _
"@hello! hi @hello #hi ##hello"})
Console.WriteLine("Mentions: {0}", String.Join(", ", FindTags("@"c, input)))
Console.WriteLine("Hashtags: {0}", String.Join(", ", FindTags("#"c, input)))
Private Function FindTags(ByVal lead As Char, ByVal source As String) As String()
Dim matches As List(Of String) = New List(Of String)
Dim current_index As Integer = 0
For index As Integer = 0 To source.Length - 2
If source(index) = lead AndAlso (index = 0 OrElse Char.IsWhiteSpace(source, index - 1)) AndAlso (Char.IsLetterOrDigit(source, index + 1) OrElse index + 1 = source.Length - 1) Then
Loop While current_index + 1 < source.Length AndAlso Char.IsLetterOrDigit(source, current_index + 1)
If current_index = source.Length - 1 OrElse Char.IsWhiteSpace(source, current_index + 1) Then
matches.Add(source.Substring(index, current_index + 1 - index))