使用正则表达式从字符串中抓取哈希标记的单词

Pat*_*son 6 c# regex

好吧,正如标题所说......我想抓住一个字符串中标记的某个单词.

示例: 这是一个#contains标签的字符串!

我想从字符串中选择单词contains作为新字符串.

我可以想象这是一个非常简单的问题,但我真的无法让它发挥作用.

tes*_*est 12

你想要这种模式有多好?理论上只是:

"(?<=#)\w+"
Run Code Online (Sandbox Code Playgroud)

会做的.

编辑,获得更多答案完整性:

string text = "This is a string that #contains a hashtag!";
var regex = new Regex(@"(?<=#)\w+");
var matches = regex.Matches(text);

foreach(Match m in matches) {
    Console.WriteLine(m.Value);
}
Run Code Online (Sandbox Code Playgroud)