build_format_regex {roxytypes} | R Documentation |
Build format regular expression
Description
Allow glue
-style formatting using keyworded regular expressions. The
original glue
string (anything that isn't expanded by glue
) is treated as
a string literal, whereas the contents of populated values can be regular
expressions, allowing for a more user-friendly way to construct complicated
regular expressions.
Usage
build_format_regex(
format,
format_re,
...,
type = re_backticked(),
description = re_any()
)
re_backticked()
re_any()
escape_non_glue_re(x)
Arguments
format |
( |
format_re |
( |
... |
Additional arguments provide keyworded capture groups for |
type |
( |
description |
( |
x |
( |
Details
To bypass glue entirely and use a standard regular expression, use
format_re
.
The provided regular expression must match all characters from the start of a
string to the end. The string also matches using "dot all" syntax, meaning
that the .
expression will also match newline characters.
Value
(character[1]:
) A regular expression string, built from component sub-expressions.
Functions
-
re_backticked()
: Match within backticks -
re_any()
: Match any -
escape_non_glue_re()
: Escape all regular expression special charactersIn addition, avoid escaping
{}
's that appear to be used asglue
keywords. Handles only simple cases, and does not handle recusive curly nesting.
Examples
re <- roxytypes:::build_format_regex(
"{as}{any}{bs}",
as = "a+",
bs = "b+",
any = ".*?"
)
roxytypes:::regex_capture(re, "aaaa\n\nbb", perl = TRUE)
text <- "@param (`test(\")\")`)"
pattern <- sprintf("`%s`", re_backticked())
m <- regexec(pattern, text, perl = TRUE)
regmatches(text, m)[[1]]
# [1] "`test(\")\")`"
# curlies escaped, as this does not appear to be a glue-style usage
roxytypes:::escape_non_glue_re(".{1,3}")
# curlies not escaped, as this is a glue-style usage
roxytypes:::escape_non_glue_re("this is a {test}")