Text Extraction
ALL-CAPS Words Regex
Uppercase words usually mean one of two things: an acronym such as API or NASA, or text that is being shouted. This pattern finds a whole word made of two or more capital ASCII letters, with word boundaries on both sides so that capitals inside a mixed-case word are ignored. Requiring at least two letters skips the pronoun I and single-letter labels. Use it to build a glossary of acronyms from a document, to flag shouty customer messages, or to check that product names follow your style guide. No flags are needed because the class itself defines the case.
Pattern (JavaScript)
/\b[A-Z]{2,}\b/Matches a word boundary, then an uppercase letter, 2 or more times, then a word boundary.
Examples
Matches
- ✓ URGENT notice
- ✓ call NASA today
- ✓ the API key
Does not match
- ✗ Hello World
- ✗ a I b
- ✗ lowercase only
Try it
3 matches (JavaScript engine)
URGENT notice call NASA today the API key Hello World a I b lowercase only
Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /\b[A-Z]{2,}\b/g;
const text = "URGENT notice";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'\b[A-Z]{2,}\b')
text = 'URGENT notice'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/\b[A-Z]{2,}\b/';
$text = 'URGENT notice';
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("\\b[A-Z]{2,}\\b");
String text = "URGENT notice";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`\b[A-Z]{2,}\b`)
text := "URGENT notice"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"\b[A-Z]{2,}\b");
var text = "URGENT notice";
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"\b[A-Z]{2,}\b").unwrap();
let text = "URGENT notice";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /\b[A-Z]{2,}\b/
text = 'URGENT notice'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why is the minimum length two letters?
- A single capital letter is often the pronoun I or the start of a sentence. Requiring two letters focuses the results on acronyms and shouted words.
- Does this match acronyms with digits like MP3 or B2B?
- No. The class only contains letters. Use \b[A-Z][A-Z0-9]+\b if you want to include digits after the first letter.
- What about capital letters with accents?
- [A-Z] covers ASCII only. With the u flag you can use \p{Lu}{2,} to match uppercase letters from any alphabet.