(?<!\w)@([A-Za-z0-9_]+) This pattern extracts @mentions of usernames: an @ not preceded by a word character, followed by letters, digits or underscores. It mirrors the hashtag pattern and avoids matching email addresses' @ fragments. Extraction patterns pull substrings out of larger text rather than judging the whole input. They usually omit anchors and use the global flag to find every occurrence, often with capturing groups to isolate the part you want. Typical uses are scraping emails, URLs, tags or mentions from prose, logs and documents, where the surrounding text is irrelevant noise. It uses the g flag(s). Apply it across text to collect every occurrence. The lookbehind prevents matching the @ inside an email (which follows a word character), reducing false positives. It does not validate that the mentioned user exists — that requires a lookup against your user directory. Platforms allowing dots or hyphens in handles need an extended character class. As with any regular expression, confirm its behavior in your target engine, test boundary and adversarial inputs, and watch for catastrophic backtracking before relying on it in production.
Token-by-token explanation
(?<!\w) — negative lookbehind: @ must not follow a word character@ — the literal at-sign([A-Za-z0-9_]+) — capture the username bodyg flag — find all mentions
What it matches
hi @ada@grace_42 and @bobcc @team What it does not match
ada@example.comabc@def@@has space When to use it
Reach for an extraction pattern when you need to pull structured tokens out of unstructured text: scraping contact details from a page, collecting links from a log, or harvesting tags from social content. Use capturing groups to isolate the part you want and the global flag to gather every occurrence. Clean the results afterward — extraction often grabs trailing punctuation or partial matches, so trim and deduplicate before storing or displaying the collected values.
Caveats
- The lookbehind prevents matching the @ inside an email (which follows a word character), reducing false positives.
- It does not validate that the mentioned user exists — that requires a lookup against your user directory.
- Platforms allowing dots or hyphens in handles need an extended character class.
Code examples
const re = /(?<!\w)@([A-Za-z0-9_]+)/g;
const text = "see hi @ada and more";
const found = [...text.matchAll(re)].map(m => m[1] ?? m[0]);
// found holds every extracted occurrence import re
rx = re.compile(r"(?<!\w)@([A-Za-z0-9_]+)")
text = "see hi @ada and more"
found = rx.findall(text) # every occurrence (groups if present) import java.util.regex.*;
Pattern p = Pattern.compile("(?<!\\w)@([A-Za-z0-9_]+)");
Matcher m = p.matcher("hi @ada");
boolean ok = m.find(); // scan for occurrences <?php
$subject = "hi @ada";
if (preg_match_all('/(?<!\w)@([A-Za-z0-9_]+)/', $subject, $m)) {
// $m[0] is the full match; $m holds every occurrence
} How to test it
Before trusting any pattern, test it against three kinds of input: valid examples that must match, invalid examples that must be rejected, and edge cases at the boundaries (empty strings, maximum lengths, unusual separators and Unicode). Paste the expression into a live regex tester with your actual sample data and confirm the highlighted matches are exactly what you intend. Watch for catastrophic backtracking by trying long non-matching inputs, and remember that engine differences (JavaScript, Python, Java, PCRE, Go/RE2) mean a pattern that works in one may fail or behave differently in another — especially lookarounds, which Go's RE2 does not support at all.
Frequently asked questions
How does it avoid matching emails?
In ada@example.com the @ follows 'ada' (a word character), so the negative lookbehind (?<!\w) rejects it, while a standalone @ada at a boundary is captured.
Is the @ included in the capture?
No — group 1 captures only the username after @. Use the full match if you need the @ symbol included.
Why do extraction patterns use the global flag?
Without g (or findall/preg_match_all), only the first occurrence is returned. The global flag iterates every match in the text, which is what you want when pulling all emails, URLs or tags from a document.
How do I capture just the part I want?
Wrap the target in a capturing group ( ... ) and read group 1 from each match. Use non-capturing groups (?: ... ) for structure you do not need, keeping the group numbering clean and predictable.
Try the Regex Tester
Live regex matching with groups, flags and explanations.