-
Notifications
You must be signed in to change notification settings - Fork 33
/
utils-stringr.R
49 lines (39 loc) · 1.46 KB
/
utils-stringr.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# stringr utils - These functions are for internal use only
str_c <- function(..., sep = "", collapse = NULL) {
paste(..., sep = sep, collapse = collapse)
}
str_flatten <- function(string, collapse = "") {
str_c(string, collapse = collapse)
}
str_detect <- function(string, pattern, fixed = FALSE, perl = FALSE) {
grepl(pattern, string, fixed = fixed, perl = perl)
}
str_extract <- function(x, pattern, fixed = FALSE, perl = FALSE) {
regmatches(x, regexpr(pattern, x, fixed = fixed, perl = perl))
}
str_extract_all <- function(x, pattern, fixed = FALSE, perl = FALSE) {
regmatches(x, gregexpr(pattern, x, fixed = fixed, perl = perl))
}
str_replace <- function(string, pattern, replacement, fixed = FALSE, perl = FALSE) {
sub(pattern, replacement, string, fixed = fixed, perl = perl)
}
str_replace_all <- function(string, pattern, replacement, fixed = FALSE, perl = FALSE) {
if (missing(replacement)) {
replacement <- unname(pattern)
pattern <- names(pattern)
} else if (length(pattern) != length(replacement)) {
if (length(pattern) > 1 & length(replacement) > 1) {
stop("replacement has to have the same length as pattern")
} else {
if (length(replacement) == 1) {
replacement <- rep(replacement, length(pattern))
} else {
pattern <- rep(pattern, length(replacement))
}
}
}
for (i in 1:length(pattern)) {
string <- gsub(pattern[[i]], replacement[[i]], string, perl = perl, fixed = fixed)
}
string
}