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.
NAgives anNAelement.- flags
A single string of flag letters, applied to every pattern:
Letter Meaning iCase-insensitive. m^and$match at line boundaries.s.matches a newline.xIgnore whitespace and allow #comments.xxAs x, and also ignore whitespace inside character classes.nOnly named groups capture. UQuantifiers are lazy unless followed by ?.JGroups may share a name. u\w,\d,\band POSIX classes follow Unicode properties.rNo case equivalence between ASCII and non-ASCII characters. aD,aS,aWKeep \d,\sor\wASCII underu.aT,aPKeep POSIX digit classes, or all POSIX classes, ASCII under u.aAll five arestrictions.Each letter may appear once, and
xandxxexclude each other.- dialect
"pcre", the default, compiles patterns as base R'sperl = TRUEdoes."js"adds the options node-pcre2 uses for JavaScript patterns:\uescapes,$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 flagsiandrapply to a literal.- jit
If
TRUE, JIT-compile each pattern when the platform allows it. If JIT compilation fails, the interpreter is used andre_info()$jitisFALSE.
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[2]>
#> [1] /(\d+)-(\d+)/i
#> [2] /^[a-z]+$/i
re_info(re)
#> pattern flags dialect literal groups group_names jit min_length prefix
#> 1 (\\d+)-(\\d+) i pcre FALSE 2 , TRUE 3
#> 2 ^[a-z]+$ i pcre FALSE 0 TRUE 1
#> backrefs pcre2_version
#> 1 FALSE 10.48
#> 2 FALSE 10.48
# A literal pattern needs no escaping
re_compile("1+1=2", literal = TRUE)
#> <re[1]>
#> [1] /1+1=2/ (literal)