This guide is for someone who already matches regular expressions
with grepl(), sub() and
regmatches(), or with stringr, and wants the zuregex call
that does the same. It gives the equivalents in two tables, then shows
the differences you would otherwise find by surprise.
The equivalent functions
Patterns that work in base R with perl = TRUE work
unchanged: both use PCRE2. stringr matches with ICU, whose syntax is
close but not the same, so check a pattern that uses anything beyond the
common core.
| Task | Base R, perl = TRUE
|
stringr | zuregex |
|---|---|---|---|
| Does it match? | grepl() |
str_detect() |
re_detect() |
| How many matches? | lengths(gregexpr()) |
str_count() |
re_count() |
| Where is the first match? | regexpr() |
str_locate() |
re_locate() |
| Where is every match? | gregexpr() |
str_locate_all() |
re_locate_all() |
| The first match |
regmatches() with regexpr()
|
str_extract() |
re_extract() |
| Every match |
regmatches() with gregexpr()
|
str_extract_all() |
re_extract_all() |
| The groups of the first match |
regmatches() with regexec(),
utils::strcapture()
|
str_match() |
re_match() |
| The groups of every match | gregexec() |
str_match_all() |
re_match_all() |
| Replace the first match | sub() |
str_replace() |
re_replace(n = 1) |
| Replace every match | gsub() |
str_replace_all() |
re_replace() |
| Split | strsplit() |
str_split() |
re_split() |
| Quote metacharacters | str_escape() |
re_escape() |
Base R’s options become arguments that every re_*()
function has.
| Base R | zuregex |
|---|---|
perl = TRUE |
always on |
ignore.case = TRUE |
flags = "i" |
fixed = TRUE |
literal = TRUE |
invert = TRUE |
re_detect(negate = TRUE) |
useBytes = TRUE |
a raw vector, or strings marked as "bytes"
|
The subject comes first and the pattern second, as in stringr. With
grepl() and sub() it is the other way
round.
Replacement replaces every match
re_replace() is gsub(), not
sub(): it replaces all the matches unless n
says otherwise. Compare the first two results, where only the year
changes, with the third.
sub("\\d+", "#", x, perl = TRUE)
#> [1] "#-10-21 GET /index.html" "no date"
re_replace(x, "\\d+", "#", n = 1)
#> [1] "#-10-21 GET /index.html" "no date"
re_replace(x, "\\d+", "#")
#> [1] "#-#-# GET /index.html" "no date"Templates use $, and know more
A replacement is a template in PCRE2’s syntax, where a group is
$1 or ${name}. Base R writes \\1
and stops at nine groups, with no names. A base R template still works,
since PCRE2 reads \\1 to \\9 as groups
too.
sub("(\\d+)-(\\d+)", "\\2/\\1", x, perl = TRUE)
#> [1] "10/2026-21 GET /index.html" "no date"
re_replace(x, "(?<year>\\d+)-(?<month>\\d+)", "${month}/${year}", n = 1)
#> [1] "10/2026-21 GET /index.html" "no date"A template can also give a default for a group that took no part in
the match. The second version has no minor number, so
${2:-0} writes 0.
re_replace(c("v1.2", "v3"), "v(\\d+)(?:\\.(\\d+))?", "major $1, minor ${2:-0}")
#> [1] "major 1, minor 2" "major 3, minor 0"?re_replace has the whole syntax, and replacement by a
function.
No match is NA, not a missing element
regmatches() drops the elements that did not match, so
its result is shorter than its input. re_extract() keeps
one result per element.
regmatches(x, regexpr("\\d+", x, perl = TRUE))
#> [1] "2026"
re_extract(x, "\\d+")
#> [1] "2026" NAGroups come back as a data frame
str_match() returns a character matrix whose first
column is the whole match. re_match() returns a data frame
of the groups only, named after them.
stringr::str_match(x, "(?<year>\\d+)-(?<month>\\d+)")
#> year month
#> [1,] "2026-10" "2026" "10"
#> [2,] NA NA NA
re_match(x, "(?<year>\\d+)-(?<month>\\d+)")
#> year month
#> 1 2026 10
#> 2 <NA> <NA>With proto it does the work of
utils::strcapture(), and the two calls give the same typed
data frame.
proto <- data.frame(year = 0L, month = 0L)
utils::strcapture("(\\d+)-(\\d+)", x, proto, perl = TRUE)
#> year month
#> 1 2026 10
#> 2 NA NA
re_match(x, "(\\d+)-(\\d+)", proto = proto)
#> year month
#> 1 2026 10
#> 2 NA NAPatterns are never recycled
Base R uses only the first pattern of several, with a warning.
zuregex follows stringr: a pattern has length one or the length of
x. Anything else is an error, here three patterns for two
strings.
Splitting keeps a trailing empty piece
strsplit() drops an empty string after the last
separator. re_split() keeps it, as Python’s
re.split() and stringr::str_split() do.
See also
?re_detect describes the arguments every function
shares. For the reasons behind these choices, read Design
and motivation.