R: R Regular Expressions

Last updated: 2026-08-26

In the previous lesson, we learned the basics of string manipulation, but 80% of real-world scenarios require pattern matching—not matching fixed strings, but matching "strings that follow a certain pattern." This is the power of regular expressions (regex).

After completing this lesson, you’ll be able to use R regular expressions to: validate phone numbers, extract email addresses, parse URLs, analyze logs, and perform text mining.

1. What You'll Learn



2. A Story About Log Analysis

(1) Pain Point: What to Do with a Million Log Entries

Alice is an operations engineer who needs to extract the URLs of all 404 errors from 1 GB of Nginx logs:

TEXT 📖 Display only
192.168.1.1 - - [15/Jan/2024:10:30:45 +0800] "GET /api/users/123 HTTP/1.1" 404 512
192.168.1.2 - - [15/Jan/2024:10:30:46 +0800] "POST /api/login HTTP/1.1" 200 1024
192.168.1.3 - - [15/Jan/2024:10:30:47 +0800] "GET /api/products/456 HTTP/1.1" 404 256
...

Trying to find 404 error all URLs in 100,000 rows is going to wear out my hands—

(2) Solution using R

R
library(stringr)

# 1. Read the log
log <- readLines("nginx.log")

# 2. Match using regular expressions 404 Incorrect URL(One line)
pattern <- '"GET (\\S+) HTTP.*" 404'
urls_404 <- str_match(log, pattern)[, 2]

# 3. Track Traffic
url_table <- table(urls_404)
sort(url_table, decreasing = TRUE)[1:10]

Handle a million log entries with just 3 lines of code. That’s the power of regular expressions.



3. Basic Regular Expression Syntax

(1) The Four Elements

100%
graph TB
    A[Regular Expressions] --> B[Literal characters<br/>abc 123]
    A --> C[Metacharacter<br/>. \\d \\s]
    A --> D[Measure Words<br/>* + ? {n}]
    A --> E[Anchor<br/>^ $ \\b]
    
    style A fill:#fff3cd
    style B fill:#cce5ff
    style C fill:#d4edda
    style D fill:#f8d7da
    style E fill:#e1d4ff

(2) Literal characters

R
# Exact match of literal characters
"cat"  # Matches "cat"
"123"  # Matches "123"

(3) Metacharacters (Core)

Metacharacter Meaning Example Match
. Any single character (except newline) a.c "abc", "axc", "a9c"
\d digit [0-9] \d+ "123", "9"
\D non-numeric \D+ "abc", "—"
\s space (space/Tab/line break) \s+ " ", "\t"
\S non-space \S+ "abc"
\w Alphanumeric characters and underscores \w+ "abc_123"
\W non- \w \W+ "—", "@"
[abc] Character (any of a, b, or c) [aeiou] Any vowel
[^abc] Non-character [^0-9] Non-numeric
[a-z] Range [a-z] Any lowercase letter
⚠️ Note: In R strings, \ is an escape sequence, so the regular expression \d must be written as "\\d" in R.

(4) Measure Words

Measure Word Meaning Example Match
* 0 times or more ab* "a", "ab", "abb"
+ 1 or more times ab+ "ab", "abb" (does not match "a")
? 0 or 1 time ab? "a", "ab"
{n} exactly n times \d{3} "123"
{n,} At least n times \d{2,} "12", "1234"
{n,m} n to m times \d{2,4} "12", "1234"

(5) Anchor

Anchor Meaning Example Match
^ Start of string ^Hello "Hello..."
$ End of string World$ "...World"
\b Word boundary \bcat\b "cat" (does not match "concatenate")


4. Grouping and Capturing

(1) Capture Group (...)

R
# Extract the year, month, and day of the date
str_match("2024-01-15", "(\\d{4})-(\\d{2})-(\\d{2})")
#      [,1]         [,2]   [,3]   [,4]
# [1,] "2024-01-15" "2024" "01"   "15"

# The first column is an exact match,Below are the capture groups

(2) Backreference \1

R
# Match Duplicate Words(e.g. "the the")
str_detect("the the cat", "\\b(\\w+)\\s+\\1\\b")
# [1] TRUE

# Match XML/HTML Tags
str_detect("<div>content</div>", "<(\\w+)>.*</\\1>")
# [1] TRUE

(3) Non-capture group (?:...)

R
# Uncaptured groups(Use ?:)
str_match("John, Smith", "(\\w+)(?:,\\s+)(\\w+)")
# [,1]         [,2]   [,3]
# "John, Smith" "John" "Smith"  ← Commas and spaces are not grouped separately


5. Greedy vs. Non-Greedy

(1) Default Greedy Algorithm

R
# Default Greedy:.* Match as many as possible
str_match("<b>text1</b><b>text2</b>", "<b>(.*)</b>")
# [,1]                          [,2]
# "<b>text1</b><b>text2</b>"    "text1</b><b>text2"
#                                 ↑ Greedy matching until the last one </b>

(2) Non-greedy *?

R
# Add ? to change to "non-greedy":.*? Match as few as possible
str_match("<b>text1</b><b>text2</b>", "<b>(.*?)</b>")
# [,1]            [,2]
# "<b>text1</b>"  "text1"  ← Matched the first one </b>
💡 Tip: Don't use regular expressions for HTML parsing—use a dedicated parser (xml2, Lesson 13). Regular expressions are best suited for simple pattern matching.



6. The stringr Regular Expression Function

(1) Function Quick Reference Table

Function Purpose Return
str_detect() Match Logical Vector
str_extract() Extract the first match Vector
str_extract_all() Extract all matches List
str_match() Extract the first match + capture group Matrix
str_match_all() Extract all matches + capture groups List
str_replace() Replace the first match Vector
str_replace_all() Replace all matches Vector
str_split() Split by Match List

(2) str_detect() Detection

R
# Which strings start with "a" Introduction
str_detect(c("apple", "banana", "cherry"), "^a")
# [1]  TRUE FALSE FALSE

# Contains numbers
str_detect(c("abc", "a1c", "123"), "\\d")
# [1] FALSE  TRUE  TRUE

(3) str_extract() Extraction

R
# Extract the number
str_extract(c("abc 123", "def 456", "no digits"), "\\d+")
# [1] "123"  "456"  NA

# Extract all numbers(List)
str_extract_all(c("a1b2c3", "no digits"), "\\d")
# [[1]] "1" "2" "3"
# [[2]] character(0)

(4) str_match() Capturing Groups

R
# Extraction Date: Year/Month/Day
str_match("2024-01-15", "(\\d{4})-(\\d{2})-(\\d{2})")
#      [,1]         [,2]   [,3]   [,4]
# [1,] "2024-01-15" "2024" "01"   "15"

# Vectorization
dates <- c("2024-01-15", "2024-02-20", "2024-03-25")
str_match(dates, "(\\d{4})-(\\d{2})-(\\d{2})")
#      [,1]         [,2]   [,3]   [,4]
# [1,] "2024-01-15" "2024" "01"   "15"
# [2,] "2024-02-20" "2024" "02"   "20"
# [3,] "2024-03-25" "2024" "03"   "25"

(5) str_replace_all() Replacement

R
# Replace all numbers with X
str_replace_all("abc 123 def 456", "\\d+", "X")
# [1] "abc X def X"

# Complex Replacements:Using capture groups
str_replace_all("John Smith", "(\\w+) (\\w+)", "\\2, \\1")
# [1] "Smith, John"  ← Last Name First


7. Hands-On: Common Validation Patterns

(1) Various validation regular expressions

R
# Cell phone number
phone_pat <- "^1[3-9]\\d{9}$"

# Email
email_pat <- "^[\\w.+-]+@[\\w.-]+\\.[a-zA-Z]{2,}$"

# ID Number(18 bit)
id_pat <- "^[1-9]\\d{5}(18|19|20)\\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\\d|3[01])\\d{3}[\\dXx]$"

# URL
url_pat <- "^https?://[\\w.-]+(:\\d+)?(/\\S*)?$"

# IPv4
ipv4_pat <- "^((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$"

# Date
date_pat <- "^\\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\\d|3[01])$"

# Test
str_detect("13800001234", phone_pat)    # TRUE
str_detect("user@example.com", email_pat)  # TRUE
str_detect("110101199003078888", id_pat)  # TRUE
str_detect("https://example.com", url_pat)  # TRUE

(2) Extract the parts of the URL

R
url <- "https://www.example.com:8080/path/to/page?query=1#section"

# Extraction Agreement,Domain Name,Port,Path,Parameters
pattern <- "^(https?)://([^:/]+)(?::(\\d+))?(/[^?#]*)?(?:\\?([^#]*))?(?:#(.*))?$"
matches <- str_match(url, pattern)
# [,1]                            [,2]      [,3]              [,4]   [,5]            [,6]      [,7]
# "https://..."                    "https"   "www.example.com" "8080" "/path/to/page" "query=1" "section"


8. Hands-On: Analyzing Nginx Logs

Below is an example of a complete workflow that ties together all the regular expression concepts covered in this lesson.

▶ Example: Analyzing Nginx Access Logs

R 📖 Display only
# ============================================
# Nginx Access Log Analysis
# Features:Parse using regular expressions 100,000 rows of log,Key Statistical Indicators
# ============================================

library(stringr)
library(dplyr)

# 1. Simulation 1000 row Nginx Log(For use in actual projects readLines Read File)
set.seed(42)
n <- 1000
ips <- paste0("192.168.", sample(1:255, n, replace = TRUE), ".",
              sample(1:255, n, replace = TRUE))
methods <- sample(c("GET", "POST", "PUT", "DELETE"), n, replace = TRUE,
                  prob = c(0.7, 0.2, 0.05, 0.05))
paths <- paste0("/api/", sample(c("users", "products", "orders", "login"),
                                n, replace = TRUE), "/",
                sample(1:1000, n, replace = TRUE))
status_codes <- sample(c(200, 201, 301, 400, 404, 500), n, replace = TRUE,
                       prob = c(0.7, 0.05, 0.05, 0.05, 0.1, 0.05))
sizes <- sample(100:5000, n)

# Concatenate into log lines
log_lines <- sprintf(
  '%s - - [15/Jan/2024:10:30:%02d +0800] "%s %s HTTP/1.1" %d %d',
  ips, sample(0:59, n, replace = TRUE), methods, paths, status_codes, sizes
)

# 2. Write to the log file
writeLines(log_lines, "nginx_demo.log")
cat("Generated 1000 Transaction Log\n")

# 3. Parsing Logs with Regular Expressions
# Format:IP - - [Time] "Methods URL Agreement" Status Code Size
log_pattern <- '^(\\S+) - - \\[([^\\]]+)\\] "(\\S+) (\\S+) (\\S+)" (\\d+) (\\d+)$'

# Extract all capture groups
matches <- str_match(log_lines, log_pattern)
colnames(matches) <- c("full", "ip", "time", "method", "path", "protocol",
                       "status", "size")

# Rotate DataFrame
logs <- as_tibble(matches) |> select(-full) |>
  mutate(
    status = as.integer(status),
    size = as.integer(size)
  )

cat("\n=== First 6 Analysis results (entries) ===\n")
print(head(logs, 6))

# 4. Analysis 1:Status Code Distribution
cat("\n=== Status Code Distribution ===\n")
status_summary <- logs |>
  group_by(status) |>
  summarise(Number of times = n(), Percentage = round(n() / nrow(logs) * 100, 2)) |>
  arrange(desc(Number of times))
print(status_summary)

# 5. Analysis 2:Find 404 All Errors URL
cat("\n=== 404 Error Top 5 ===\n")
errors_404 <- logs |>
  filter(status == 404) |>
  group_by(path) |>
  summarise(Number of times = n()) |>
  arrange(desc(Number of times)) |>
  head(5)
print(errors_404)

# 6. Analysis 3:Each IP traffic
cat("\n=== Top 5 Active IP ===\n")
top_ips <- logs |>
  group_by(ip) |>
  summarise(
    Number of visits = n(),
    Number of errors = sum(status >= 400),
    Total Traffic = sum(size)
  ) |>
  arrange(desc(Number of visits)) |>
  head(5)
print(top_ips)

# 7. Analysis 4:Hourly traffic
cat("\n=== Visits per minute(First 10 minutes)===\n")
logs <- logs |>
  mutate(minute = str_sub(time, 15, 16))

per_minute <- logs |>
  group_by(minute) |>
  summarise(Number of visits = n(), Number of errors = sum(status >= 400)) |>
  head(10)
print(per_minute)

# 8. Retrieve Email Address(If the log contains an email address)
text_with_emails <- "Contact alice@example.com or bob@test.org for support."
email_pat <- "[\\w.+-]+@[\\w.-]+\\.[a-zA-Z]{2,}"
emails <- str_extract_all(text_with_emails, email_pat)
cat("\n=== Extracted email addresses ===\n")
print(emails)

# 9. Cleanup
file.remove("nginx_demo.log")
70 logic lines (exceeds 40-line limit, display only)

Expected Output (Excerpt):

TEXT 📖 Display only
=== Status Code Distribution ===
# A tibble: 6 × 3
  status  Number of times Percentage
   <int> <int> <dbl>
1    200   698    70
2    404   105    10
3    201    52     5
...

=== Top 5 Active IP ===
# A tibble: 5 × 4
  ip           Number of visits Number of errors Total Traffic
  <chr>            <int>  <int>  <int>
1 192.168.97.1        8      1   18499
2 192.168.52.244      7      2   18438
...

❓ FAQ

Q Are R regular expressions POSIX or Perl-style?
A stringr uses the ICU regular expression library (similar to Perl) and supports \d \s \w shorthand. Standard R grep uses POSIX ERE ([[:digit:]]). The stringr style is recommended.
Q \\d Why are there two backslashes?
A The R string "\\d" is actually a regular expression \d—R parses \\ as a single \. Similarly, "\\s" is actually a regular expression \s.
Q How do I choose between greedy and non-greedy matching?
A The default is greedy, which matches the maximum number of characters. This can cause issues when parsing HTML, XML, or JSON; add ? to switch to non-greedy matching. For simple matches, greedy matching is sufficient.
Q What is the difference between str_match and str_extract?
A str_extract returns only the first match; str_match returns the complete match plus each capture group. Use str_match to retrieve capture groups.
Q How do I handle Chinese characters?
A In R 4.x UTF-8, use the literals [\u4e00-\u9fff] or "Chinese" directly. For GBK files, convert them to UTF-8 first.
Q What can I do if regular expression matching is slow?
A ① Avoid .* greediness (use .*?) ② Precompile complex patterns ③ Use stringi::stri_detect_regex() for faster performance (C++ backend) |

📖 Summary


📝 Exercises

  1. Basic Problem: Use a regular expression to extract all amounts (in the format "Price: $99.50, Quantity: 3 pcs, Total: $298.50") from the string "Price: $99.50, Quantity: 3 pcs, Total: $298.50", and output a list of numbers.

  2. Basic Problem: Use a regular expression to verify whether the following data conforms to the email format: ① user@example.comuser.name+tag@sub.example.co.ukinvalid@@example.com. Output a boolean vector.

  3. Basic Problem: Use a regular expression to extract all dates from "Today is 2024-01-15, weather Sunny; Tomorrow is 2024-01-16, Cloudy" and output a character vector.

  4. Advanced Exercise: Simulate 100 "order confirmation emails" (containing order numbers, amounts, dates, and product names), use regular expressions to extract these four fields into a data frame, and verify that each one has been successfully extracted.

  5. Challenge: Write a script to read 1,000 lines of Nginx access logs and use regular expressions to: ① parse the fields in each line (IP/time/method/URL/status code/size); ② list the top 10 URLs with 4xx errors; ③ identify the IP address with the highest access frequency; ④ calculate the average hourly traffic. Take a screenshot and save the analysis results.

Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏