ore_switch: String multiplexing

ore_switchR Documentation

String multiplexing

Description

This function maps one character vector to another, based on sequential matching to a series of regular expressions. The return value corresponding to each element in the source text is chosen based on the first matching regex: once matched, later options are ignored.

Usage

ore_switch(text, ..., options = "", encoding = getOption("ore.encoding"))

Arguments

text

A vector of strings to match against.

...

One or more string arguments specifying a possible return value. These are generally named with a regex, and the string is only used for a given text element if the regex matches (and no previous one matched). These strings may reference captured groups. Unnamed arguments match unconditionally, and will always be taken literally.

options

A string composed of characters indicating variations on the usual interpretation of the regex. These may currently include "i" for case-insensitive matching, and "m" for multiline matching (in which case "." matches the newline character).

encoding

A string specifying the encoding that matching will take place in. The default is given by the "ore.encoding" option, which is usually set automatically from the current locale when the package is loaded, but can be modified if needed.

Value

A character vector of the same length as text, containing the multiplexed strings. If none of the regexes matched, the corresponding element will be NA.

See Also

ore_subst for details of back-reference syntax.

Examples

# Extract digits where present; otherwise return zero
ore_switch(c("2 dogs","no dogs"), "\\d+"="\\0", "0")

jonclayden/ore documentation built on Jan. 13, 2024, 2:54 a.m.