tools / regex library / twitter handle
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

InputResult
@v0iddomatches
@elonmuskmatches
@amatches
@with-dashrejects
@toolongusernamefor_xrejects
no-at-signrejects

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

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  

Related

uuid · uk postcode · sql injection keywords · ipv4 address