stringi (version 0.2-3)

stri_replace_all_regex: Replace Occurrences of a Regex Pattern Matches

Description

Replaces with the given replacement string every/first/last substring of the input that matches a regular expression

Usage

stri_replace_all_regex(str, pattern, replacement, opts_regex = NULL)

stri_replace_first_regex(str, pattern, replacement, opts_regex = NULL)

stri_replace_last_regex(str, pattern, replacement, opts_regex = NULL)

Arguments

str
character vector of strings to search in
pattern
character vector of regular expressions
replacement
character vector of strings to replace with, possibly including references to capture groups
opts_regex
a named list with ICU Regex settings as generated with stri_opts_regex; NULL for default settings

Value

  • Each function returns a character vector.

Details

Vectorized over str, pattern, and replacement.

These functions scan the input string for matches of the pattern. Input that is not part of any match is left unchanged; each match is replaced in the result by the replacement string.

The replacement string may contain references to capture groups (in round parentheses). References are of the form $n, where n is the number of the capture group (their numbering starts from 1).

See Also

Other search_regex: stri_count_regex; stri_detect_regex; stri_extract_all_regex, stri_extract_first_regex, stri_extract_first_regex, stri_extract_last_regex, stri_extract_last_regex; stri_locate_all_regex, stri_locate_first_regex, stri_locate_first_regex, stri_locate_last_regex, stri_locate_last_regex; stri_match_all_regex, stri_match_first_regex, stri_match_first_regex, stri_match_last_regex, stri_match_last_regex; stri_opts_regex; stri_split_regex; stringi-search-regex; stringi-search

Other search_replace: stri_replace_all_charclass, stri_replace_first_charclass, stri_replace_first_charclass, stri_replace_last_charclass, stri_replace_last_charclass; stri_replace_all_coll, stri_replace_first_coll, stri_replace_first_coll, stri_replace_last_coll, stri_replace_last_coll; stri_replace_all_fixed, stri_replace_first_fixed, stri_replace_first_fixed, stri_replace_last_fixed, stri_replace_last_fixed; stri_replace_all; stri_replace_first; stri_replace_last; stri_replace_na; stri_replace; stringi-search

Examples

Run this code
s <- "Lorem ipsum dolor sit amet, consectetur adipisicing elit."
stri_replace_all_regex(s, ".*? ", "#")
stri_replace_all_regex(s, "(el|s)it", "1234")
stri_replace_all_regex('abaca', 'a', c('!', '*'))
stri_replace_all_regex('123|456|789', '(\\p{N}).(\\p{N})', '$2-$1')
stri_replace_all_regex(c("stringi R", "REXAMINE", "123"), '( R|R.)', ' r ')

Run the code above in your browser using DataLab