DevKits

Extraction

Match an @-mention

Flags: g

Extract @username mentions from text (Twitter, GitHub, etc).

Pattern

Regular expression

@[A-Za-z0-9_]{1,39}

How it works

Matches `@` followed by 1–39 word characters (the maximum GitHub / Twitter handle length). Underscores allowed, no hyphens.

Matches

  • @octocat
  • @user_name
  • @a

Non-matches

  • email@example.com (`@` inside word)
  • @ (empty)

Common gotchas

To avoid matching `@` inside email addresses, prepend a word boundary and require the `@` to be at the start of a token: `(?:^|[^\w])@[A-Za-z0-9_]{1,39}` and trim the leading non-word char.

Language snippets

JavaScript

const re = /@[A-Za-z0-9_]{1,39}/g;

Python

re.findall(r"@[A-Za-z0-9_]{1,39}", text)

Go

re := regexp.MustCompile(`@[A-Za-z0-9_]{1,39}`)

Java

Pattern.compile("@[A-Za-z0-9_]{1,39}")

Related patterns

Try any pattern live in the Regex Tester