Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match word beginning with @

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 @ contained within them

e.g. @help me@ ple@se @now

matches at Index: 0 Length 5, Index: 13 Length 3, Index: 17 Length 4

This shouldn't match at Index 13 should it?

like image 395
Matt Wilko Avatar asked Dec 08 '22 11:12

Matt Wilko


1 Answers

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

like image 68
falsetru Avatar answered Dec 11 '22 07:12

falsetru