Social
Subreddit reference (r/name) Regex
On Reddit, communities are referenced as r/name or /r/name, and the site turns those into links automatically. This pattern finds such references at the start of text or after whitespace or an opening parenthesis, so it ignores words like car/rental that merely contain r/. Subreddit names are 3 to 21 characters of letters, digits and underscores and cannot begin with an underscore, which the pattern follows. It suits comment analysis, community dashboards and link auto-formatting. It cannot know whether a subreddit exists, is private or has been banned, and it does not read full reddit.com URLs.
Pattern (JavaScript)
/(?:^|[\s(])/?r/[A-Za-z0-9][A-Za-z0-9_]{2,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 "r/", then a letter or digit, then one of the characters A-Za-z0-9_, between 2 and 20 times, then a word boundary.
Examples
Matches
- ✓ Check r/regex for help
- ✓ /r/learnprogramming
- ✓ (see r/test_sub)
- ✓ r/abc
Does not match
- ✗ r/ab
- ✗ car/rental prices
- ✗ u/test_user
- ✗ r/_hidden
Try it
4 matches (JavaScript engine)
Check r/regex for help /r/learnprogramming (see r/test_sub) r/abc r/ab car/rental prices u/test_user r/_hidden
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(])\/?r\/[A-Za-z0-9][A-Za-z0-9_]{2,20}\b/g;
const text = "Check r/regex for help";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'(?:^|[\s(])/?r/[A-Za-z0-9][A-Za-z0-9_]{2,20}\b')
text = 'Check r/regex for help'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '#(?:^|[\s(])/?r/[A-Za-z0-9][A-Za-z0-9_]{2,20}\b#';
$text = 'Check r/regex for help';
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(])/?r/[A-Za-z0-9][A-Za-z0-9_]{2,20}\\b");
String text = "Check r/regex for help";
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(])/?r/[A-Za-z0-9][A-Za-z0-9_]{2,20}\b`)
text := "Check r/regex for help"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"(?:^|[\s(])/?r/[A-Za-z0-9][A-Za-z0-9_]{2,20}\b");
var text = "Check r/regex for help";
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(])/?r/[A-Za-z0-9][A-Za-z0-9_]{2,20}\b").unwrap();
let text = "Check r/regex for help";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /(?:^|[\s(])\/?r\/[A-Za-z0-9][A-Za-z0-9_]{2,20}\b/
text = 'Check r/regex for help'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why must the name be at least three characters?
- Reddit requires subreddit names of 3 to 21 characters. Short references like r/ab cannot be real communities, so the pattern treats them as plain text.
- Does it match full reddit.com/r/ links?
- Not by default, because the r/ there follows a dot-com and slash rather than whitespace. Use a URL pattern for full links, or add a slash to the allowed preceding characters.
- How do I get the name without the r/ prefix?
- Put a capture group around [A-Za-z0-9][A-Za-z0-9_]{2,20} and read that group. With the g flag you can collect every community mentioned in a thread.