Skip to contents

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

(character[1]) A glue-style format string. Expanded whisker values are used as a shorthand for capture groups, where ellipsis arguments can be provided for additional capture group patterns.

format_re

(character[1]) Alternatively, provide a standard regular expression directly.

...

Additional arguments provide keyworded capture groups for format

type

(character[1]) A regular expression to use to match a type signature. By default, matches within backticks.

description

(character[1]) A regular expression to use to match a parameter description. By default, matches any string.

x

(character[1]) A string to escape.

Value

(character[1]:) A regular expression string, built from component sub-expressions.

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.

Functions

  • re_backticked(): Match within backticks

  • re_any(): Match any

  • escape_non_glue_re(): Escape all regular expression special characters

    In addition, avoid escaping {}'s that appear to be used as glue 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)
#>      as     any    bs  
#> [1,] "aaaa" "\n\n" "bb"

text <- "@param (`test(\")\")`)"

pattern <- sprintf("`%s`", re_backticked())

m <- regexec(pattern, text, perl = TRUE)
regmatches(text, m)[[1]]
#> [1] "`test(\")\")`"
# [1] "`test(\")\")`"

# curlies escaped, as this does not appear to be a glue-style usage
roxytypes:::escape_non_glue_re(".{1,3}")
#> [1] "\\.\\{1,3\\}"

# curlies not escaped, as this is a glue-style usage
roxytypes:::escape_non_glue_re("this is a {test}")
#> [1] "this is a {test}"