Pattern library / URLs & Marketing
Mention Regex (@handles)
Mentions look simple — @ plus a handle — until a paragraph of email addresses lights up as matches. This page gives the plain pattern, the boundary variant that behaves in RE2 engines (no lookbehind!), and the platform notes that decide whether @user-name is one handle or two.
How the pattern works
[A-Za-z0-9_]+— the handle charset most platforms use (Twitter/X, GitHub, Instagram core rules). Dots and hyphens vary by platform; extend the class per source.- Capture the handle:
@([A-Za-z0-9_]+)— group 1 drops the @ for storage or counting. - RE2-safe word boundary (lookbehind
(?<![\w.])is rejected by RE2 and GA4):(^|[^A-Za-z0-9_.])@([A-Za-z0-9_]+)— group 1 proves no word character precedes the @. - Case: handles are case-insensitive on most platforms — compare lowercased values in code, not case-sensitively in the regex.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
Hey @countoftuscany, nice post | Match | finds @countoftuscany | Try → |
write ada@example.com for help | Match | matches @example — email overlap is real (pitfalls) | Try → |
@user-name rocks | Match | stops at the hyphen: @user only | Try → |
price @ $5 | No match | handle must start right after @ | Try → |
at sign only @ | No match | nothing after the @ | Try → |
contact us today | No match | no @ at all | Try → |
Where to use it
JavaScript
const all = text.match(/@[A-Za-z0-9_]+/g) || []; // boundary version — skips emails: const re = /(^|[^A-Za-z0-9_.])@([A-Za-z0-9_]+)/g;
Python
import re handles = re.findall(r'@([A-Za-z0-9_]+)', text) # with boundary: real = [m.group(2) for m in re.finditer(r'(^|[^\w.])@([A-Za-z0-9_]+)', text)]
BigQuery / GA4 (RE2)
WHERE REGEXP_CONTAINS(msg, r'(^|[^A-Za-z0-9_.])@support_team\b')
Common mistakes
- Email overlap:
ada@example.comcontains@example— the plain pattern matches it. Use the boundary variant, or strip emails before scanning for mentions. (?<!lookbehind works in JavaScript and Python but is not RE2 — GA4, BigQuery and Go reject it outright. The(^|[^…])group trick above is the portable substitute.- Platform variance: GitHub allows hyphens (
@octo-cat), emails allow dots — extend the class per source instead of shipping one mega-class that fits nobody.
FAQ
How do I match a mention but not an email?
Anchor the @ to a non-word boundary: (^|[^A-Za-z0-9_.])@([A-Za-z0-9_]+). Group 1 guarantees nothing word-like precedes the @, which emails always violate (local part or dot).
Why is my lookbehind pattern rejected in BigQuery?
RE2 has no lookarounds by design (that is what guarantees linear time). Replace the lookbehind with the group trick from the notes — same result, valid RE2.
How do I extract all mentions from a message?
@([A-Za-z0-9_]+) with a global search: JavaScript match(/@([A-Za-z0-9_]+)/g), Python findall(r'@([A-Za-z0-9_]+)', text).
Related patterns
Hashtag Regex - Match #tags in Posts & Text · Email Validation Regex — Practical Pattern to Test · Slug Regex — Match lowercase-hyphen URL Slugs
For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.