Package 'zuregex'

Title: Bundled 'PCRE2' Regular Expressions with a C Interface
Description: Matches regular expressions with a bundled copy of the 'PCRE2' library (<https://github.com/PCRE2Project/pcre2>), so that every platform runs the same version with its just-in-time compiler. Patterns are compiled once and cached, matching runs on several threads, and capture groups are returned as character vectors and data frames built in C. Replacements use the full 'PCRE2' substitution language, and each call can bound its work with match, depth and heap limits. Other packages reach the same library from C through LinkingTo, instead of bundling their own copy.
Authors: Pedro Baltazar [aut, cre, cph], Philip Hazel [ctb, cph] (Author of PCRE2, bundled in src/vendor/pcre2), University of Cambridge [cph] (PCRE2, bundled in src/vendor/pcre2), Zoltan Herczeg [ctb, cph] (Author of the PCRE2 JIT compiler and sljit, bundled in src/vendor/pcre2), Nicholas Wilson [ctb] (PCRE2 maintainer, bundled in src/vendor/pcre2), PCRE2 contributors [cph] (see inst/COPYRIGHTS)
Maintainer: Pedro Baltazar <[email protected]>
License: MIT + file LICENSE
Version: 0.1.0
Built: 2026-12-01 09:56:51 UTC
Source: https://github.com/pedrobtz/zuregex

Help Index


The compiled-pattern cache

Description

Every ⁠re_*()⁠ function compiles a character pattern through a cache of recently used compiled patterns, so a pattern used again, in a loop or across calls, is not recompiled. Patterns are keyed by their text, flags, dialect, literal and jit. The least recently used pattern is dropped when the cache is full. Patterns longer than 65,536 characters are never cached.

Usage

re_cache_size(n = NULL)

re_cache_clear()

Arguments

n

NULL to query the capacity, or a whole number from 0 to 256 to set it. 0 turns the cache off. Shrinking the cache drops the least recently used patterns.

Details

re objects from re_compile() stay valid when the cache drops their pattern or is cleared.

Value

re_cache_size() returns the capacity; when setting it, the previous capacity, invisibly. re_cache_clear() returns the number of patterns it dropped, invisibly.

Examples

re_cache_size()
old <- re_cache_size(16)
re_cache_size(old)
re_cache_clear()

Compile regular expressions

Description

re_compile() compiles each pattern once, with PCRE2's just-in-time compiler where the platform allows it, and returns an re vector that every ⁠re_*()⁠ function accepts in place of a character pattern. Compilation goes through the pattern cache, so compiling the same pattern again is cheap; see re_cache_size().

Usage

re_compile(
  pattern,
  flags = "",
  dialect = c("pcre", "js"),
  literal = FALSE,
  jit = TRUE
)

Arguments

pattern

A character vector of regular expressions. NA gives an NA element.

flags

A single string of flag letters, applied to every pattern:

Letter Meaning
i Case-insensitive.
m ^ and $ match at line boundaries.
s . matches a newline.
x Ignore whitespace and allow ⁠#⁠ comments.
xx As x, and also ignore whitespace inside character classes.
n Only named groups capture.
U Quantifiers are lazy unless followed by ⁠?⁠.
J Groups may share a name.
u ⁠\w⁠, ⁠\d⁠, ⁠\b⁠ and POSIX classes follow Unicode properties.
r No case equivalence between ASCII and non-ASCII characters.
aD, aS, aW Keep ⁠\d⁠, ⁠\s⁠ or ⁠\w⁠ ASCII under u.
aT, aP Keep POSIX digit classes, or all POSIX classes, ASCII under u.
a All five a restrictions.

Each letter may appear once, and x and xx exclude each other.

dialect

"pcre", the default, compiles patterns as base R's perl = TRUE does. "js" adds the options node-pcre2 uses for JavaScript patterns: ⁠\u⁠ escapes, $ only at the very end, and unset back-references that match the empty string.

literal

If TRUE, each pattern is matched as a literal string, with no metacharacters. Only the flags i and r apply to a literal.

jit

If TRUE, JIT-compile each pattern when the platform allows it. If JIT compilation fails, the interpreter is used and re_info()$jit is FALSE. A pattern with a variable-length lookbehind, such as ⁠(?<=a?)⁠, also uses the interpreter, because PCRE2's JIT matches some of them wrongly. A match that needs more stack than the JIT has is retried in the interpreter, so heap in re_limits() bounds it.

Value

An re vector, as long as pattern.

See Also

re_info() to inspect compiled patterns, re_escape() to quote metacharacters.

Examples

re <- re_compile(c("(\\d+)-(\\d+)", "^[a-z]+$"), flags = "i")
re
re_info(re)

# A literal pattern needs no escaping
re_compile("1+1=2", literal = TRUE)

Cap the size of a compiled pattern

Description

A short pattern can compile to a very large program: counted repeats of a group copy the group, so nested ones multiply. A 77-character pattern with three nested {253} repeats compiles to 458 MB. zuregex refuses any pattern whose compiled form would exceed the cap, with a zuregex_error_compile, before PCRE2 allocates the memory.

Usage

re_compile_size(bytes = NULL)

Arguments

bytes

NULL to query the cap, or a whole number of bytes, at least 1, to set it. Inf turns the cap off.

Details

The default, 16 MiB, is about a hundred times the largest realistic pattern: an alternation of keywords at the cache's 65,536-character limit compiles to about 150 KB. The cap bounds compiling only; to bound matching, use re_limits().

The cap applies to every compile in the session, including compiles through the C API. A pattern already in the cache keeps working when the cap is lowered; re_cache_clear() drops it. An re object's pattern is compiled again for bytes subjects and after readRDS(), and that compile is held to the cap of the moment, so an object made under a higher cap may refuse raw subjects once the cap is lowered.

Value

The cap in bytes. When setting it, the previous cap, invisibly.

Examples

re_compile_size()
try(re_compile("(?:(?:(?:ab|cd){200}){200}){200}"))
old <- re_compile_size(2^30)
re_compile_size(old)

Detect and count matches

Description

re_detect() reports whether each element of x matches its pattern. re_count() counts the non-overlapping matches in each element.

Usage

re_detect(
  x,
  pattern,
  anchor = c("none", "start", "full"),
  negate = FALSE,
  flags = "",
  literal = FALSE,
  limits = NULL
)

re_count(x, pattern, flags = "", literal = FALSE, limits = NULL)

Arguments

x

A character vector, a raw vector, or a list of raw vectors. NA (or NULL in a list) gives NA.

pattern

A character vector or an re vector; see Patterns.

anchor

Where a match must be: "none" anywhere, "start" at the start of the subject (Python's re.match()), "full" the whole subject (Python's re.fullmatch()). Anchoring is done by PCRE2, so it holds for every branch of a top-level alternation.

negate

If TRUE, return TRUE for elements that do not match.

flags

A single string of flag letters for pattern strings, such as "i" for case-insensitive matching; see re_compile() for every letter. It applies to every pattern in the call.

literal

If TRUE, each pattern string is matched as literal text, with no metacharacters, as base R's fixed = TRUE. Only the flags i and r apply to a literal pattern.

limits

A re_limits() object, or NULL for getOption("zuregex.limits") and then PCRE2's defaults.

Value

re_detect(): a logical vector, one per subject. re_count(): an integer vector, one per subject.

Patterns

pattern is a character vector of regular expressions, compiled through the cache with flags and literal, or an re vector from re_compile(), which carries its own flags and can use the JavaScript dialect. It has length 1, used for every element of x, or exactly the length of x, pairing pattern[i] with x[i]. Any other length is an error: nothing is recycled. An NA pattern gives NA for its element.

Base R's options map onto these arguments: ignore.case = TRUE is flags = "i", fixed = TRUE is literal = TRUE, invert = TRUE is re_detect(negate = TRUE), and useBytes = TRUE is a raw vector or strings marked as "bytes" (see Strings and bytes). perl = TRUE is always on.

Strings and bytes

Character input is matched as UTF-8; strings in other declared encodings are translated first. Invalid UTF-8 is an error of class zuregex_error_utf. A raw vector is one subject matched as bytes, as is each element of a list of raw vectors, and so is a character vector with any element marked as "bytes". Patterns are then compiled without UTF-8 support, so . matches one byte. ⁠\\C⁠, which matches one byte, is allowed only then: in a UTF-8 pattern it is a compile error, since it could split a character. A bytes pattern may not turn UTF-8 back on with ⁠(*UTF)⁠.

Examples

x <- c("apple", "banana", "cherry", NA)
re_detect(x, "an")
re_detect(x, "a|b", anchor = "start")
re_count(x, "[aeiou]")
re_detect(x, "APPLE", flags = "i")
re_count("a.b.c", ".", literal = TRUE)

# One pattern per element
re_detect(c("abc", "abc"), c("^a", "^b"))

Quote regular expression metacharacters

Description

Escapes every character that has a meaning in a PCRE2 pattern, so that the result matches x literally, as Go's QuoteMeta() does. The result is also literal under the x flag, which gives whitespace and ⁠#⁠ a meaning. To match a whole pattern literally, literal = TRUE in re_compile() is simpler.

Usage

re_escape(x)

Arguments

x

A character vector. NA stays NA.

Value

A character vector as long as x, in UTF-8.

Examples

re_escape("1+1=2 (really?)")

Extract matches and capture groups

Description

re_extract() returns the first match in each element of x, or one of its groups; re_extract_all() returns every match. re_match() returns every group of the first match as a data frame with one row per element; re_match_all() returns one row per match. The strings are built in C, without the per-match R overhead of regmatches() or utils::strcapture().

Usage

re_extract(x, pattern, group = 0, flags = "", literal = FALSE, limits = NULL)

re_extract_all(
  x,
  pattern,
  group = 0,
  n = Inf,
  flags = "",
  literal = FALSE,
  limits = NULL
)

re_match(x, pattern, proto = NULL, flags = "", literal = FALSE, limits = NULL)

re_match_all(x, pattern, n = Inf, flags = "", literal = FALSE, limits = NULL)

Arguments

x

A character vector, a raw vector, or a list of raw vectors. NA (or NULL in a list) gives NA.

pattern

A character vector or an re vector; see Patterns.

group

The group to extract: a number, 0 for the whole match, or a group name.

flags

A single string of flag letters for pattern strings, such as "i" for case-insensitive matching; see re_compile() for every letter. It applies to every pattern in the call.

literal

If TRUE, each pattern string is matched as literal text, with no metacharacters, as base R's fixed = TRUE. Only the flags i and r apply to a literal pattern.

limits

A re_limits() object, or NULL for getOption("zuregex.limits") and then PCRE2's defaults.

n

The most matches to return per element, Inf for all.

proto

NULL for character columns, or a data frame or list with one element per group, as for utils::strcapture(): each column is converted with ⁠as.<class>()⁠ for the class of the matching element, and takes its name. S4 classes are not supported.

Value

re_extract(): a character vector, NA where there is no match. re_extract_all(): a list of character vectors, NA for an NA subject or pattern.

re_match(): a data frame with one row per subject and one column per group, all NA where there is no match. re_match_all(): a data frame with element and match columns, then one column per group, one row per match; a subject with no match has no rows, and an NA subject or pattern one row of NA. A group named element or match repeats that column's name, so $ gives the leading column; index the group by position instead.

Strings from character input are in UTF-8; from bytes they are marked as "bytes".

Groups and columns

Groups are numbered from 1; group 0 is the whole match. In re_match() a named group's column takes its name and an unnamed group's column its number, so ⁠(?<year>\\d+)-(\\d+)⁠ gives the columns year and 2. A group that did not take part in the match is NA.

With one pattern per element, the columns are the union of the patterns' groups in order of first appearance: groups with the same name share a column, unnamed groups share a column by position, and an element whose pattern lacks a group has NA there.

Patterns

pattern is a character vector of regular expressions, compiled through the cache with flags and literal, or an re vector from re_compile(), which carries its own flags and can use the JavaScript dialect. It has length 1, used for every element of x, or exactly the length of x, pairing pattern[i] with x[i]. Any other length is an error: nothing is recycled. An NA pattern gives NA for its element.

Base R's options map onto these arguments: ignore.case = TRUE is flags = "i", fixed = TRUE is literal = TRUE, invert = TRUE is re_detect(negate = TRUE), and useBytes = TRUE is a raw vector or strings marked as "bytes" (see Strings and bytes). perl = TRUE is always on.

Strings and bytes

Character input is matched as UTF-8; strings in other declared encodings are translated first. Invalid UTF-8 is an error of class zuregex_error_utf. A raw vector is one subject matched as bytes, as is each element of a list of raw vectors, and so is a character vector with any element marked as "bytes". Patterns are then compiled without UTF-8 support, so . matches one byte. ⁠\\C⁠, which matches one byte, is allowed only then: in a UTF-8 pattern it is a compile error, since it could split a character. A bytes pattern may not turn UTF-8 back on with ⁠(*UTF)⁠.

Examples

x <- c("2026-10-21", "1999-01-02 and 2000-03-04", "none", NA)
re_extract(x, "\\d{4}")
re_extract(x, "(?<year>\\d{4})-(\\d{2})", group = "year")
re_extract_all(x, "\\d{4}")

re_match(x, "(?<year>\\d{4})-(?<month>\\d{2})")
re_match(x, "(\\d{4})-(\\d{2})", proto = data.frame(year = 0L, month = 0L))
re_match_all(x, "(?<year>\\d{4})")

Describe compiled patterns

Description

Reports what PCRE2 recorded when it compiled each pattern.

Usage

re_info(re)

Arguments

re

An re vector from re_compile().

Value

A data frame with one row per pattern and columns:

  • pattern, flags, dialect, literal: what was compiled. flags is rebuilt from PCRE2's options, in a canonical order.

  • groups: the number of capture groups.

  • group_names: a list column; for each pattern, a character vector with one name per group, "" for unnamed groups.

  • jit: whether the pattern runs JIT-compiled code.

  • min_length: a lower bound on the length of a match, in characters.

  • prefix: a literal string every match starts with, "" when there is none or it cannot be determined simply. It may be shorter than the longest such string.

  • backrefs: whether the pattern has back-references.

  • pcre2_version: the PCRE2 version that compiled it.

An NA pattern gives a row of NA.

Examples

re_info(re_compile(c("(?<year>\\d{4})-(\\d{2})", "abc+")))

Bound the work a match may do

Description

PCRE2 matches by backtracking, so a pattern such as ⁠^(a+)+$⁠ can take time exponential in the length of the subject. zuregex makes no linear-time promise; for that, use the re2 package. What it promises is that limits bound the work per element, and that hitting one is either a classed error or an NA you asked for.

Usage

re_limits(match = NULL, depth = NULL, heap = NULL, on_limit = c("error", "na"))

Arguments

match

The most times PCRE2's internal match function may be called for one search; PCRE2's default is 10,000,000. The JIT compiler honours it too.

depth

The deepest backtracking nesting for one search; PCRE2's default equals match. The JIT compiler ignores it.

heap

The most heap memory, in kibibytes, the interpreter may use for backtracking in one search; PCRE2's default is 20,000,000. The JIT compiler ignores it.

on_limit

What an element that hits a limit gives: "error" raises a zuregex_error_limit naming the limit and the element; "na" gives NA for that element and one warning of class zuregex_warning_limit for the call, counting how many did.

Details

Pass re_limits() as ⁠limits =⁠ to any matching function, or set it for the session with options(zuregex.limits = re_limits(...)). The argument wins over the option. NULL keeps PCRE2's default for that limit.

Value

An object of class re_limits.

Examples

# No match, but only after trying every way to split the a's
x <- paste0(strrep("a", 30), "!")
try(re_detect(x, "^(a+)+$", limits = re_limits(match = 1000)))
re_detect(
  c(x, "aaa"),
  "^(a+)+$",
  limits = re_limits(match = 1000, on_limit = "na")
)

old <- options(zuregex.limits = re_limits(match = 1e5))
options(old)

Locate matches

Description

re_locate() finds the first match in each element of x; re_locate_all() finds every match.

Usage

re_locate(
  x,
  pattern,
  bytes = FALSE,
  flags = "",
  literal = FALSE,
  limits = NULL
)

re_locate_all(
  x,
  pattern,
  n = Inf,
  overlap = FALSE,
  bytes = FALSE,
  flags = "",
  literal = FALSE,
  limits = NULL
)

Arguments

x

A character vector, a raw vector, or a list of raw vectors. NA (or NULL in a list) gives NA.

pattern

A character vector or an re vector; see Patterns.

bytes

If TRUE, offsets count bytes instead of characters. A string in another encoding, such as latin1, is matched as UTF-8, so the bytes counted are those of its UTF-8 form.

flags

A single string of flag letters for pattern strings, such as "i" for case-insensitive matching; see re_compile() for every letter. It applies to every pattern in the call.

literal

If TRUE, each pattern string is matched as literal text, with no metacharacters, as base R's fixed = TRUE. Only the flags i and r apply to a literal pattern.

limits

A re_limits() object, or NULL for getOption("zuregex.limits") and then PCRE2's defaults.

n

The most matches to return per element, Inf for all.

overlap

If TRUE, each search starts one character after the start of the previous match, so matches may overlap.

Details

Offsets are one-based and inclusive, as regexpr() and stringr::str_locate() report them, so substr(x, start, end) extracts a match, and for raw input so does x[start:end]. An empty match at position k has start = k and end = k - 1. Offsets count characters, or bytes with bytes = TRUE; for raw input the two are the same.

After an empty match, the next search follows the pattern's dialect: for "pcre" PCRE2's rule, which first tries for a non-empty match at the same position, and for "js" JavaScript's, which moves on one character.

Value

re_locate(): an integer matrix with columns start and end and one row per subject, NA where there is no match.

re_locate_all(): a data frame with one row per match and columns element (the subject's index), match (the match's number within the element), start and end. A subject with no match has no rows; an NA subject or pattern has one row with match, start and end NA.

Patterns

pattern is a character vector of regular expressions, compiled through the cache with flags and literal, or an re vector from re_compile(), which carries its own flags and can use the JavaScript dialect. It has length 1, used for every element of x, or exactly the length of x, pairing pattern[i] with x[i]. Any other length is an error: nothing is recycled. An NA pattern gives NA for its element.

Base R's options map onto these arguments: ignore.case = TRUE is flags = "i", fixed = TRUE is literal = TRUE, invert = TRUE is re_detect(negate = TRUE), and useBytes = TRUE is a raw vector or strings marked as "bytes" (see Strings and bytes). perl = TRUE is always on.

Strings and bytes

Character input is matched as UTF-8; strings in other declared encodings are translated first. Invalid UTF-8 is an error of class zuregex_error_utf. A raw vector is one subject matched as bytes, as is each element of a list of raw vectors, and so is a character vector with any element marked as "bytes". Patterns are then compiled without UTF-8 support, so . matches one byte. ⁠\\C⁠, which matches one byte, is allowed only then: in a UTF-8 pattern it is a compile error, since it could split a character. A bytes pattern may not turn UTF-8 back on with ⁠(*UTF)⁠.

Examples

x <- c("apple pie", "banana", NA)
loc <- re_locate(x, "an+")
loc
substr(x, loc[, "start"], loc[, "end"])

re_locate_all(x, "[aeiou]")
re_locate_all("aaaa", "aa", overlap = TRUE)

Replace matches

Description

re_replace() replaces matches in each element of x, all of them by default. re_expand() returns a template expanded with each element's first match, as Python's Match.expand() does.

Usage

re_replace(
  x,
  pattern,
  replacement,
  n = Inf,
  syntax = c("extended", "literal"),
  flags = "",
  literal = FALSE,
  limits = NULL
)

re_expand(x, pattern, template, flags = "", literal = FALSE, limits = NULL)

Arguments

x

A character vector, a raw vector, or a list of raw vectors. NA (or NULL in a list) gives NA.

pattern

A character vector or an re vector; see Patterns.

replacement

A character vector of templates, of length 1 or the length of x, or a function; see Templates and Replacement functions. An NA template gives NA.

n

The most matches to replace per element, Inf for all.

syntax

"extended" or "literal"; see Templates.

flags

A single string of flag letters for pattern strings, such as "i" for case-insensitive matching; see re_compile() for every letter. It applies to every pattern in the call.

literal

If TRUE, each pattern string is matched as literal text, with no metacharacters, as base R's fixed = TRUE. Only the flags i and r apply to a literal pattern.

limits

A re_limits() object, or NULL for getOption("zuregex.limits") and then PCRE2's defaults.

template

A character vector of templates, of length 1 or the length of x.

Value

A character vector as long as x. Elements without a match are returned unchanged by re_replace() and as NA by re_expand(). Other strings are in UTF-8, or marked as "bytes" for bytes input.

Templates

With syntax = "extended", the default, templates use PCRE2's extended substitution syntax:

Template Meaning
⁠$1⁠, ⁠${12}⁠, ⁠${name}⁠ A group by number, past nine, or by name. ⁠$0⁠ is the whole match.
⁠\1⁠, ⁠\g<1>⁠, ⁠\g<name>⁠ A group, as in base R or Python.
⁠${1:-none}⁠ The group, or none when it did not take part in the match.
⁠${1:+yes:no}⁠ yes when the group took part, otherwise no.
⁠\U...\E⁠, ⁠\L...\E⁠, ⁠\u⁠, ⁠\l⁠ Upper or lower case until ⁠\E⁠, or for one character.
⁠$$⁠, ⁠\$⁠ A literal $.

A backslash and digits are read as in a pattern. So ⁠\12⁠ is group 12 only when the pattern has 12 groups, and otherwise the octal escape for a newline. Prefer ⁠${12}⁠ past nine. In an R string each backslash is doubled, as in "\\1".

A group that did not take part in the match is replaced by "". A reference to a group that does not exist, or a malformed template, raises zuregex_error_template when a match first uses it. With syntax = "literal" the replacement is inserted as it is.

Replacement functions

replacement may be a function. It is called once for the whole call, if anything matched, with a data frame of every match: element, match (its number within the element), 0 (the matched text) and one column per group, named as in re_match(). A group named element, match or 0 repeats that column's name, as in re_match_all(). It must return a character vector with one replacement per row.

Patterns

pattern is a character vector of regular expressions, compiled through the cache with flags and literal, or an re vector from re_compile(), which carries its own flags and can use the JavaScript dialect. It has length 1, used for every element of x, or exactly the length of x, pairing pattern[i] with x[i]. Any other length is an error: nothing is recycled. An NA pattern gives NA for its element.

Base R's options map onto these arguments: ignore.case = TRUE is flags = "i", fixed = TRUE is literal = TRUE, invert = TRUE is re_detect(negate = TRUE), and useBytes = TRUE is a raw vector or strings marked as "bytes" (see Strings and bytes). perl = TRUE is always on.

Strings and bytes

Character input is matched as UTF-8; strings in other declared encodings are translated first. Invalid UTF-8 is an error of class zuregex_error_utf. A raw vector is one subject matched as bytes, as is each element of a list of raw vectors, and so is a character vector with any element marked as "bytes". Patterns are then compiled without UTF-8 support, so . matches one byte. ⁠\\C⁠, which matches one byte, is allowed only then: in a UTF-8 pattern it is a compile error, since it could split a character. A bytes pattern may not turn UTF-8 back on with ⁠(*UTF)⁠.

Examples

x <- c("2026-10-21", "no date", NA)
re_replace(x, "(\\d{4})-(\\d{2})-(\\d{2})", "$3/$2/$1")
re_replace(x, "(?<y>\\d{4})", "${y}!")
re_replace("hello world", "(\\w+)", "\\u$1")
re_replace("a1b22", "\\d+", function(m) strrep("#", nchar(m[["0"]])))
re_expand("John Smith", "(\\w+) (\\w+)", "$2, $1")

Split strings

Description

Splits each element of x at the matches of pattern, as Python's re.split() does: an empty match splits too, and with keep_groups = TRUE each separator's groups are kept between the pieces.

Usage

re_split(
  x,
  pattern,
  n = Inf,
  keep_groups = FALSE,
  flags = "",
  literal = FALSE,
  limits = NULL
)

Arguments

x

A character vector, a raw vector, or a list of raw vectors. NA (or NULL in a list) gives NA.

pattern

A character vector or an re vector; see Patterns.

n

The most pieces per element, Inf for no limit. The last piece holds the rest of the string, as with Go's Split(n); n = 0 gives no pieces.

keep_groups

If TRUE, the groups of each separator are inserted after the piece before it, NA for a group that did not take part.

flags

A single string of flag letters for pattern strings, such as "i" for case-insensitive matching; see re_compile() for every letter. It applies to every pattern in the call.

literal

If TRUE, each pattern string is matched as literal text, with no metacharacters, as base R's fixed = TRUE. Only the flags i and r apply to a literal pattern.

limits

A re_limits() object, or NULL for getOption("zuregex.limits") and then PCRE2's defaults.

Value

A list as long as x of character vectors; NA for an NA subject or pattern.

Patterns

pattern is a character vector of regular expressions, compiled through the cache with flags and literal, or an re vector from re_compile(), which carries its own flags and can use the JavaScript dialect. It has length 1, used for every element of x, or exactly the length of x, pairing pattern[i] with x[i]. Any other length is an error: nothing is recycled. An NA pattern gives NA for its element.

Base R's options map onto these arguments: ignore.case = TRUE is flags = "i", fixed = TRUE is literal = TRUE, invert = TRUE is re_detect(negate = TRUE), and useBytes = TRUE is a raw vector or strings marked as "bytes" (see Strings and bytes). perl = TRUE is always on.

Strings and bytes

Character input is matched as UTF-8; strings in other declared encodings are translated first. Invalid UTF-8 is an error of class zuregex_error_utf. A raw vector is one subject matched as bytes, as is each element of a list of raw vectors, and so is a character vector with any element marked as "bytes". Patterns are then compiled without UTF-8 support, so . matches one byte. ⁠\\C⁠, which matches one byte, is allowed only then: in a UTF-8 pattern it is a compile error, since it could split a character. A bytes pattern may not turn UTF-8 back on with ⁠(*UTF)⁠.

Examples

re_split(c("a, b,c", "d"), ",\\s*")
re_split("a1b22c", "(\\d+)", keep_groups = TRUE)
re_split("a-b-c-d", "-", n = 2)

Thread cap for matching

Description

Matching runs on several threads with OpenMP when a call's subjects add up to more than an internal size threshold, and on one thread below it. re_threads() reports the most threads a call may use; re_threads(n) caps it for the rest of the session. The cap applies only to zuregex's own matching, not to other OpenMP code in the process.

Usage

re_threads(n = NULL)

Arguments

n

NULL to query the effective maximum, a whole number of at least 1 to cap the threads, or 0 to remove the cap. Values above the machine's thread count are allowed and are the same as no cap.

Details

Results never depend on the number of threads, so this is a performance control, not a reproducibility one. In a build without OpenMP the value is always 1 and a cap has no effect; zuregex_info() reports which build this is.

A forked child, such as a worker of parallel::mclapply(), matches on one thread, because GNU OpenMP can hang in a child whose parent has used it.

Value

Querying returns the effective maximum as an integer. Setting returns the previous cap invisibly, 0 meaning none was set, so ⁠old <- re_threads(1); re_threads(old)⁠ restores it exactly.

Examples

re_threads()
old <- re_threads(1)
re_threads(old)

Describe the bundled PCRE2 library

Description

Reports the version of PCRE2 that zuregex was built with, whether its just-in-time (JIT) compiler works on this machine, and how matching is set up: OpenMP, the threads and cores it can use, the input size it starts at, and the cache size.

Usage

zuregex_info()

Details

The JIT compiler can be built in and still fail at run time, on a CPU it does not support or where the system refuses executable memory. PCRE2 then matches with its interpreter, which gives the same results more slowly. jit_usable tells the two cases apart by JIT-compiling a small pattern.

Value

A list with elements:

  • pcre2_version: the bundled PCRE2 version, such as "10.49".

  • pcre2_date: its release date.

  • unicode: whether Unicode and UTF-8 support is built in.

  • unicode_version: the Unicode version of PCRE2's character tables.

  • jit: whether the JIT compiler is built in.

  • jit_usable: whether the JIT compiler works on this machine.

  • jit_target: the architecture the JIT compiler generates code for, or NA without it.

  • openmp: whether this build matches on several threads. On macOS that needs the OpenMP headers from https://mac.r-project.org/openmp/ when zuregex is built.

  • threads: the most threads a call may use; see re_threads().

  • cores: the processors OpenMP sees, or NA without OpenMP.

  • serial_bytes: the total size of a call's subjects, in bytes, below which it runs on one thread, because starting threads would cost more than they save.

  • cache_size: the pattern cache's capacity; see re_cache_size().

Examples

zuregex_info()