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 / Clause | Requirement | Data Type | Role & Behavior |
|---|---|---|---|
[@#] | Required | Prefix Character Class | Matches either a social mention symbol (@) or topic hashtag symbol (#) at the beginning of each token. |
[a-zA-Z0-9_]+ | Required | Identifier Body | Captures letters, numbers, and underscores forming the handle or topic tag until whitespace or punctuation. |
(...) Capture Group | Required | Group 1 Wrapper | Wraps the combined prefix and body in parentheses so replacement templates ($1) or group extractors pull the full tag. |
g (Global Flag) | Optional | Regex Modifier | Continues 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
- Regular Expression for Email Validation & Extraction — Validate and extract email addresses with a clean regular expression. Test against valid and invalid emails live with capture groups.
- Extract Numbers and Decimals From Text Using Regex — Extract integers, decimals, and prices from mixed text strings using Regex. Test live with interactive match highlighting.
- Validate YYYY-MM-DD ISO Dates with Regular Expressions — Validate and extract strict YYYY-MM-DD calendar dates with Regex, rejecting invalid months (>12) and days (>31). Test live.
- Strong Password Validation Regex Using Lookaheads — Enforce strong password complexity with positive lookaheads in Regex (8+ characters, uppercase, lowercase, digit, symbol). Test live.