regex pattern · ready to copy
Regex for matching Twitter / X handles (@username)
Match @-prefixed Twitter/X handles (1-15 alphanumeric + underscore).
intermediate
javascript / pcre / python4 use cases
The pattern
@[A-Za-z0-9_]{1,15}\b
Test cases
| Input | Result |
|---|---|
| @v0iddo | ✓matches |
| @elonmusk | ✓matches |
| @a | ✓matches |
| @with-dash | ✗rejects |
| @toolongusernamefor_x | ✗rejects |
| no-at-sign | ✗rejects |
Edge cases & caveats
Twitter has reserved usernames + length grew over time (was 15, still capped). For email addresses inside text, the `@` is preceded by alphanumeric — add `(?<!\w)` lookbehind to avoid matching the `@user` part of `me@user.com`.
Common use cases
- social mention extraction
- tweet content scraping
- influencer discovery
- @-mention parsing
Try variations against your data
regexlab is a free in-browser tester with side-by-side match highlighting, group inspector, and named-capture export to JS/Python/PCRE.
Open regexlab