Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grabing hashtagged word from a string using regex

Tags:

c#

regex

Well, as the title says... I want to grab a certain word that is hashtagged in a string.

Example: This is a string that #contains a hashtag!

I want to pick out the word contains from the string as a new string.

I can imagine that this is a very simple problem, but I really can't get it to work.

like image 368
pthorsson Avatar asked Dec 13 '12 22:12

pthorsson


1 Answers

How good do you want this pattern to be? In theory just:

"(?<=#)\w+"

would do it.

Edit, for more answer completeness:

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);
}
like image 160
test Avatar answered Sep 29 '22 09:09

test