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}")

Frequently asked questions

What is the regex for @-mention?

The pattern @[A-Za-z0-9_]{1,39} (flags: g) matches @-mention. Matches `@` followed by 1–39 word characters (the maximum GitHub / Twitter handle length). Underscores allowed, no hyphens.

What does this @-mention pattern match?

It matches strings like @octocat, @user_name, @a, but rejects email@example.com (`@` inside word), @ (empty).

How do I use this pattern in JavaScript, Python, Go, Java?

Ready-to-run snippets are provided for JavaScript, Python, Go, Java. Copy the one for your language from the Language snippets section — each wraps the same core pattern in that language's regex API.

What are common pitfalls with a @-mention regex?

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.

Related patterns

Try this pattern in a tool

Try any pattern live in the Regex Tester