Skip to main content

Anchors, Classes, and Quantifiers

Practical reference for the regex features you'll use most often in day-to-day rg searches.

Line Anchors

# Lines that START with "FATAL"
rg "^FATAL" app.log

# Lines that END with "timeout"
rg "timeout$" app.log

# Empty lines
rg "^$" config.yaml

# Lines with ONLY whitespace
rg "^\s+$" script.sh

Character Classes

# Any digit
rg "\d+" data.csv # Rust engine shorthand
rg "[0-9]+" data.csv # POSIX equivalent (more portable)

# Word characters (letters, digits, underscore)
rg "\w+" identifiers.txt

# Whitespace
rg "\s{2,}" text.md # 2+ consecutive whitespace chars

# Custom set: vowels
rg "[aeiouAEIOU]" words.txt

# Negated set: NOT a digit
rg "[^0-9]" numbers.txt

POSIX Classes (portable across platforms)

ClassMatches
[[:alpha:]]Letters
[[:digit:]]Digits 0–9
[[:alnum:]]Letters and digits
[[:space:]]Whitespace
[[:upper:]]Uppercase letters
[[:lower:]]Lowercase letters

Quantifiers

# Zero or one (optional)
rg "colou?r" # matches "color" and "colour"

# One or more
rg "go+" # matches "go", "goo", "gooo"

# Zero or more
rg "https?://" # matches "http://" and "https://"

# Exactly N
rg "[0-9]{4}" # exactly 4 digits (e.g. year)

# Between N and M
rg "[0-9]{2,4}" # 2 to 4 digits

# At least N
rg "[a-f0-9]{32,}" # MD5 or longer hash

Practical Patterns for Logs

# IPv4 address
rg "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" access.log

# ISO 8601 date
rg "[0-9]{4}-[0-9]{2}-[0-9]{2}" events.log

# HTTP status codes
rg "HTTP/[0-9\.]+ [45][0-9]{2}" nginx.log

# UUID
rg "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}" trace.log

# Email address (simplified)
rg "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b" contacts.csv