Regex Guide · Prompt: “Match hashtags (#) and user mentions (@) containing letters, numbers, or underscores”

Extract Hashtags and @Mentions Using Regex

Verified by LiveFormulaLab Engineering Team • Updated November 2026

Extract social media hashtags and @mentions from text posts with Regex. Interactive live tester with capture groups included.

Verified Regex Syntax

([@#][a-zA-Z0-9_]+)

How This Formula Works (Step-by-Step)

Social listening exports and community posts mix @mentions and #hashtags directly inside prose.

Step 1: [@#] matches either a literal "@" or "#" trigger character.

Step 2: [a-zA-Z0-9_]+ captures all valid handle or tag characters immediately following the prefix.

Formula Syntax & Arguments Breakdown

Matches either @ mention or # hashtag trigger prefixes followed immediately by one or more alphanumeric or underscore identifier characters, ignoring trailing punctuation marks like commas and periods.

Argument / ClauseRequirementData TypeRole & Behavior
[@#]RequiredPrefix Character ClassMatches either a social mention symbol (@) or topic hashtag symbol (#) at the beginning of each token.
[a-zA-Z0-9_]+RequiredIdentifier BodyCaptures letters, numbers, and underscores forming the handle or topic tag until whitespace or punctuation.
(...) Capture GroupRequiredGroup 1 WrapperWraps the combined prefix and body in parentheses so replacement templates ($1) or group extractors pull the full tag.
g (Global Flag)OptionalRegex ModifierContinues scanning after the first token so posts containing multiple mentions and hashtags extract every match.

3 Real-World Use Cases

  • Analyzing trending #hashtags inside social campaign CSV exports.
  • Extracting @mentions from Slack or Discord community threads.
  • Building clickable tag links in user-generated content renderers.

Cross-Platform Equivalents

Excel
=REGEXEXTRACT(A2, "([@#][a-zA-Z0-9_]+)")
SQL
SELECT REGEXP_MATCHES(post, '([@#][a-zA-Z0-9_]+)', 'g') FROM social_posts;
Python Pandas
df['tags'] = df['post'].str.findall(r'([@#][a-zA-Z0-9_]+)')

Common Mistakes & Pro Tips

Pitfalls & How to Fix Them

  • Mistake: Running ([@#][a-zA-Z0-9_]+) on raw text containing email addresses matches "@liveformulalab" out of "user@liveformulalab.com".

    Fix: Require whitespace or start-of-line before the prefix: (?:^|\s)([@#][a-zA-Z0-9_]+).

  • Mistake: Using \w+ without realizing Unicode handles (like accented names) fail ASCII classes in older regex engines.

    Fix: Use [\p{L}\p{N}_]+ with the u flag in modern JavaScript/Python when international hashtags are expected.

  • Mistake: Forgetting the global (g) flag only extracts the very first hashtag in a multi-tag post.

    Fix: Keep the g flag enabled (or use str.findall in Python) to collect every tag on the line.

Engineering Pro Tips

  • Normalize extracted hashtags with .toLowerCase() before counting campaign frequency so #Excel and #excel merge.
  • To capture CSS/GitHub hyphenated tags like #power-bi, add a hyphen inside the character class: ([#@][a-zA-Z0-9_-]+).
  • Wrap matches in Markdown or HTML anchor links during replace operations using Group 1 ($1).
  • In Google Sheets, extract all hashtags into a single comma-separated summary using TEXTJOIN(", ", TRUE, REGEXEXTRACT(A2, "([@#][a-zA-Z0-9_]+)")).

Frequently Asked Questions

Why does this pattern ignore email addresses like bob@acme.com?
To strictly ignore emails, add a boundary or space check before the prefix: (?:^|\s)([@#][a-zA-Z0-9_]+).
How do I match only hashtags and not mentions?
Remove @ from the character class: (#[a-zA-Z0-9_]+).
How do I allow hyphens inside hashtags?
Include a hyphen inside the character class: ([#][a-zA-Z0-9_-]+).

Related Formulas & Guides