I am trying to develop some regex to find all words that start with an @:
I thought that \\@\\w+
would do it but this also matches words that have @ con
And what about a non-Regex approach?
C# version:
string input = "word1 word2 @word3 ";
string[] resultWords = input.Split(' ').ToList().Where(x => x.Trim().StartsWith("@")).ToArray();
VB.NET version:
Dim input As String = "word1 word2 @word3 "
Dim resultWords() As String = input.Split(" "c).ToList().Where(Function(x) x.Trim().StartsWith("@")).ToArray
How about a negative look-behind:
(?<!\w)@\w+
Try using
(?<=^|\s)@\w+
Can't remember if c# allows alternation in a look behind
RegExr
Use \B@\w+
(non-word boundary).
For example:
string pattern = @"\B@\w+";
foreach (var match in Regex.Matches(@"@help me@ ple@se @now", pattern))
Console.WriteLine(match);
output:
@help
@now
BTW, you don't need to escape @
.
http://ideone.com/nsT015