regexhelper
Test a pattern. Copy a ready-made one. No signup.

Regex to Match a Hashtag

Copy the pattern, paste your text below, and see every hashtag it finds. Below the tester: why it skips number-only tags and how to allow Unicode.

/ /

What this pattern matches

This matches a hashtag: a # that starts a word, a first character that is a letter or underscore, and then any word characters. Number-only tags like #100 are skipped.

How it works, part by part

(?<!\w) — a lookbehind so the # is not preceded by a word character (skips a#b).

#[A-Za-z_] — the hash and a required first letter or underscore.

\w* — the rest of the tag: letters, digits, or underscores.

Unicode hashtags

To match non-Latin tags like #café or #日本, use the Unicode flag and property classes (\p{L}) where your engine supports them.

FAQ

Why is #100 ignored?

The first character after # must be a letter or underscore, so a purely numeric tag does not match.

Does it match a # in the middle of a word?

No. The lookbehind requires the # to start a word, so a#b is skipped.

How do I match emoji or non-Latin hashtags?

Use the u flag with \p{L} character properties in engines that support Unicode property escapes.