Regex to match word beginning with @

前端 未结 4 1141
长情又很酷
长情又很酷 2021-01-13 22:21

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

4条回答
  •  悲&欢浪女
    2021-01-13 22:55

    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

提交回复
热议问题