regex_sub: Substitute characters matching a regular expression.

Description Usage Arguments Details Value Examples

View source: R/haircut.R

Description

Given a vector of character strings, substitute parts of the strings matching 'pattern' with 'replacement'. 'replacement' can be a vector argument, in which case it must be the same length as 'text', and the substiuted 'text' is made per 'replacement' string.

Usage

1
regex_sub(text, pattern, replacement, sub_all = TRUE)

Arguments

text

a vector of character strings to have patterns substituted.

pattern

a string pattern that, if matched in 'text' will be substituded with 'replacement'.

replacement

either a string or vector of strings to replace matching patterns in 'text'.

sub_all

Determined if all matches are substited (default), or the first ocurrence per input string.

Details

The default mode is to substitute all pattern matches. This can be switched to first match only by setting 'sub_all = FALSE'.

Value

a vector of character strings

Examples

1
2
3
4
5
## Not run: 
regex_sub(c("ABC", "AXYZ", "DEFA"),"A", "_")
regex_sub(c("ABC_", "AXY_", "DEF_"),"_", c(1,2,3)

## End(Not run)

MilesMcBain/haircut documentation built on May 21, 2019, 2:31 a.m.