Regex to find words that start with a specific character

Try this #(\S+)\s?


Match a word starting with # after a white space or the beginning of a line. The last word boundary in not necessary depending on your usage.

/(?:^|\s)\#(\w+)\b/

The parentheses will capture your word in a group. Now, it depends on the language how you apply this regex.

The (?:...) is a non-capturing group.


Search for:

  • something that is not a word character then
  • #
  • some word characters

So try this:

/(?<!\w)#\w+/

Or in C# it would look like this:

string s = "Lorem ipsum #text Second lorem ipsum. How #are You. It's ok. Done. Something #else now.";
foreach (Match match in Regex.Matches(s, @"(?<!\w)#\w+"))
{
    Console.WriteLine(match.Value);
}

Output:

#text
#are
#else

Tags:

C#

Regex