We take a few lines of a web server’s log and work through them: find
the lines that match, pull out parts of them, turn them into a data
frame, rewrite them and cut them up. After that you know the main
re_*() functions and the rules they share. Moving from base
R or stringr, limits on untrusted patterns and the C API each have their
own article, listed at the end.
The log is a character vector. Its last element is not a log line, so we can see what a string that does not match gives.
log <- c(
"2026-10-21 08:15:02 GET /index.html 200 512",
"2026-10-21 08:15:09 POST /login 302 0",
"2026-10-21 08:16:44 GET /missing 404 128",
"not a log line"
)Every function takes the strings first, as x, and then a
pattern: a regular expression in PCRE2’s syntax, which
is the syntax of base R’s perl = TRUE.
re_detect() says which elements match.
One pattern is used for every element. The other choice is one
pattern per element, as long as x. Here each line is tested
for its own status code, and only the second line has the one asked
for.
A pattern of any other length is an error: zuregex never recycles one.
re_extract() returns the text of the first match in each
element, and NA where there is none.
Parentheses in a pattern make a group, a part of the
match you can ask for by itself. group picks one, by number
or, for a group written (?<name>...), by name. This
returns the status code alone, without the spaces the pattern needs
around it.
re_match() returns every group at once, as a data frame
with one row per element. A named group gives its name to its
column.
pattern <- "(?<time>\\S+ \\S+) (?<method>[A-Z]+) (?<path>\\S+) (?<status>\\d+)"
re_match(log, pattern)
#> time method path status
#> 1 2026-10-21 08:15:02 GET /index.html 200
#> 2 2026-10-21 08:15:09 POST /login 302
#> 3 2026-10-21 08:16:44 GET /missing 404
#> 4 <NA> <NA> <NA> <NA>The columns are character. proto gives them types, as it
does for utils::strcapture(): a data frame whose columns
are examples of the type wanted. Look at status, now an
integer.
proto <- data.frame(time = "", method = "", path = "", status = 0L)
str(re_match(log, pattern, proto = proto))
#> 'data.frame': 4 obs. of 4 variables:
#> $ time : chr "2026-10-21 08:15:02" "2026-10-21 08:15:09" "2026-10-21 08:16:44" NA
#> $ method: chr "GET" "POST" "GET" NA
#> $ path : chr "/index.html" "/login" "/missing" NA
#> $ status: int 200 302 404 NAre_match() looks at the first match in each element.
re_match_all() returns a row for every match, with
element and match saying where each came from.
Each of the first three lines has two numbers at its end, and the last
line has no rows.
re_replace() replaces every match with a
template, in which ${name} or
$1 stands for a group. Here the date is turned round, and
the line with no date comes back unchanged.
re_replace(log, "^(?<y>\\d+)-(?<m>\\d+)-(?<d>\\d+)", "${d}/${m}/${y}")
#> [1] "21/10/2026 08:15:02 GET /index.html 200 512"
#> [2] "21/10/2026 08:15:09 POST /login 302 0"
#> [3] "21/10/2026 08:16:44 GET /missing 404 128"
#> [4] "not a log line"Templates can do more than this, such as defaults and case changes;
?re_replace lists the syntax.
re_split() cuts each element at the matches, and returns
a list with a character vector of pieces per element.
So far each pattern was a string. re_compile() turns one
into an re object, which carries its flags
with it and which every function takes in place of the string. The flag
i ignores case, so this lower-case pattern finds the log’s
GET.
get <- re_compile("\\bget\\b", flags = "i")
get
#> <re[1]>
#> [1] /\bget\b/i
re_detect(log, get)
#> [1] TRUE FALSE TRUE FALSEYou do not need re_compile() for speed. Pattern strings
go through a cache, so a string used again is not compiled again; see
?re_cache_size.
The most common mistake is a pattern that is not valid. This one never closes the group it opens.
re_match(log, "(?<time>\\S+ \\S+ (?<method>[A-Z]+)")
#> Error in `re_match()`:
#> ! Can't compile pattern: missing closing parenthesis at character 34.
#> (?<time>\S+ \S+ (?<method>[A-Z]+)
#> ^The error names the problem and marks where in the pattern PCRE2
found it. Like every error zuregex raises, it has a class, here
zuregex_error_compile, so tryCatch() can
handle it.