正则表达式匹配以@开头的单词 [英] Regex to match word beginning with @

查看:1407
本文介绍了正则表达式匹配以@开头的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一些正则表达式,以查找所有以@开头的单词:

I am trying to develop some regex to find all words that start with an @:

我认为 \ @ \w + 可以做到,但这也匹配其中包含@的单词

I thought that \@\w+ would do it but this also matches words that have @ contained within them

例如
@help me @ ple @ se @now

在$code处匹配:0长度5,索引:13长度3,索引:17长度4

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

这应该与索引13不匹配吗?

This shouldn't match at Index 13 should it?

推荐答案

使用 \B @ \w + (非单词边界) )。

Use \B@\w+ (non-word boundary).

例如:

string pattern = @"\B@\w+";
foreach (var match in Regex.Matches(@"@help me@ ple@se @now", pattern))
    Console.WriteLine(match);

输出:

@help
@now

BTW,您不需要转义 @

BTW, you don't need to escape @.

http://ideone.com/nsT015

这篇关于正则表达式匹配以@开头的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆