Python (re)

Twitter / X Handle in PY

Match Twitter/X @handles — 1 to 15 characters of letters, digits, or underscores preceded by @.

Try it in the PY tester →

Pattern

regexPY
@([A-Za-z0-9_]{1,15})\b   (flags: g)

Python (re) code

pyPython
import re

pattern = re.compile(r"@([A-Za-z0-9_]{1,15})\b")
input_text = "Follow @jack and @TwitterDev for updates"
for m in pattern.finditer(input_text):
    print(m.group(0))

Stdlib `re` module — no third-party dependency. Works on Python 3.6+.

How the pattern works

@ matches the literal at-sign. The capturing group ([A-Za-z0-9_]{1,15}) captures 1–15 characters of the Twitter username alphabet. \b prevents partial matches inside longer words.

Examples

Input

Follow @jack and @TwitterDev for updates

Matches

  • @jack
  • @TwitterDev

Input

@user_123 liked your post

Matches

  • @user_123

Input

no handles here

No match

Same pattern, other engines

← Back to Twitter / X Handle overview (all engines)