我正在尝试开发一些正则表达式来查找以@开头的所有单词:
我以为\@\w+
可以做到这一点,但这也可以匹配其中包含@的单词
例如
@help me@ ple@se @now
在匹配 Index: 0 Length 5, Index: 13 Length 3, Index: 17 Length 4
这不应该与索引13相匹配吗?
使用\B@\w+
(非单词边界)。
例如:
string pattern = @"\B@\w+"; foreach (var match in Regex.Matches(@"@help me@ ple@se @now", pattern)) Console.WriteLine(match);
输出:
@help @now
顺便说一句,您不需要逃脱@
。
http://ideone.com/nsT015