Social
Reddit user reference (u/name) Regex
Reddit users are mentioned as u/name or /u/name, which pings the user and links to their profile. Reddit usernames are 3 to 20 characters of letters, digits, underscores and hyphens. This pattern finds those mentions at the start of the text or after whitespace or an opening parenthesis, which keeps it from matching paths such as menu/items. It is useful when analysing comment threads, building notification features for a Reddit-style forum or anonymising exports. It does not check whether the account exists or has been suspended, and it does not handle full reddit.com/user/ URLs.
Pattern (JavaScript)
/(?:^|[\s(])/?u/[A-Za-z0-9_-]{3,20}\b/Matches (the start of the line/string OR one of the characters \s(), then the character "/", optionally (zero or one time), then the text "u/", then one of the characters A-Za-z0-9_-, between 3 and 20 times, then a word boundary.
Examples
Matches
- ✓ thanks u/test_user
- ✓ /u/Sample-Redditor
- ✓ (by u/abc)
- ✓ u/dev_42 said hi
Does not match
- ✗ u/ab
- ✗ menu/items
- ✗ r/test_user
- ✗ no users here
Try it
4 matches (JavaScript engine)
thanks u/test_user /u/Sample-Redditor (by u/abc) u/dev_42 said hi u/ab menu/items r/test_user no users here
Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /(?:^|[\s(])\/?u\/[A-Za-z0-9_-]{3,20}\b/g;
const text = "thanks u/test_user";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'(?:^|[\s(])/?u/[A-Za-z0-9_-]{3,20}\b')
text = 'thanks u/test_user'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '#(?:^|[\s(])/?u/[A-Za-z0-9_-]{3,20}\b#';
$text = 'thanks u/test_user';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("(?:^|[\\s(])/?u/[A-Za-z0-9_-]{3,20}\\b");
String text = "thanks u/test_user";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?:^|[\s(])/?u/[A-Za-z0-9_-]{3,20}\b`)
text := "thanks u/test_user"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"(?:^|[\s(])/?u/[A-Za-z0-9_-]{3,20}\b");
var text = "thanks u/test_user";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"(?:^|[\s(])/?u/[A-Za-z0-9_-]{3,20}\b").unwrap();
let text = "thanks u/test_user";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /(?:^|[\s(])\/?u\/[A-Za-z0-9_-]{3,20}\b/
text = 'thanks u/test_user'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- What characters can Reddit usernames contain?
- Letters, digits, underscores and hyphens, from 3 to 20 characters. Spaces and dots are not allowed, so the character class stops at the first such character.
- Why is the preceding character restricted?
- Requiring start of text, whitespace or an opening parenthesis prevents false hits inside paths and words like menu/items. Without it, any text containing u/ followed by letters would match.
- How can I anonymise usernames in an export?
- Use this pattern with the g flag in a replace call and substitute a placeholder such as u/[user]. Keep the captured leading character in the replacement so spacing is preserved.