diff --git a/NAMESPACE b/NAMESPACE index 649c895..2946ae0 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -17,12 +17,15 @@ export(col_max) export(col_min) export(col_sum) export(combine_overlapping_date_range) +export(del_pattern) export(dolock) export(draw_xlsx) export(emonth) export(equal) export(fill_one_before_first_one) export(fill_zero_not_first_pos) +export(get_pattern) +export(get_pattern_all) export(grepl_and) export(has_cols) export(loadRDS) @@ -39,8 +42,6 @@ export(paste_list) export(paste_sort_uni_str) export(paste_str) export(paste_uni_str) -export(pull_str) -export(pull_str_all) export(replace_empty_with_na) export(replace_na_with_empty) export(replace_na_with_zero) diff --git a/R/str.R b/R/str.R index 4931c07..5ef945d 100644 --- a/R/str.R +++ b/R/str.R @@ -57,29 +57,30 @@ split_str <- function(x, split = "|") { z[!z %in% c(NA, "NA", "")] } -#' Pull a first string having a specific pattern. +#' Get a first pattern #' -#' Pull a first string having a specific pattern. +#' Get a first pattern from a string vector. #' #' @param pattern a string containing a [regular expression] #' @param x a string vector #' @param ignore.case if `FALSE`, the pattern matching is case sensitive and if `TRUE`, case is ignored during matching. #' @return a string vector +#' #' @examples -#' # pull a first string having a specific pattern -#' \donttest{pull_str(pattern = "c", c("a|b|c", "a|c|c"))} +#' # get a first pattern from a string vector +#' \donttest{get_pattern(pattern = "c", c("a|b|c", "a|c|c"))} #' #' @export -pull_str <- function(pattern, x, ignore.case = TRUE) { +get_pattern <- function(pattern, x, ignore.case = TRUE) { r <- regexpr(pattern, x, ignore.case = ignore.case, perl = TRUE) z <- rep("", length(x)) z[r != -1] <- regmatches(x, r) return(z) } -#' Pull all the strings having a specific pattern. +#' Get all patterns #' -#' Pull all the strings having a specific pattern. +#' Get all patterns from a string vector. #' #' @param pattern a string containing a [regular expression] #' @param x a string vector @@ -88,16 +89,30 @@ pull_str <- function(pattern, x, ignore.case = TRUE) { #' ([`character`] of length 1). default "|" #' @param ignore.case if `FALSE`, the pattern matching is case sensitive and if `TRUE`, case is ignored during matching. #' @return a string vector +#' #' @examples -#' # pull all the strings having a specific pattern -#' \donttest{pull_str_all(pattern = "c", c("a|b|c", "a|c|c"))} +#' # get all patterns from a string vector +#' \donttest{get_pattern_all(pattern = "c", c("a|b|c", "a|c|c"))} #' #' @export -pull_str_all <- function(pattern, x, collapse = "|", ignore.case = TRUE) { +get_pattern_all <- function(pattern, x, collapse = "|", ignore.case = TRUE) { r <- gregexpr(pattern, x, ignore.case = ignore.case, perl = TRUE) z <- regmatches(x, r) sapply(z, function(s) paste(s, collapse = collapse)) } -remove_str <- function(pattern, x) +#' Delete patterns +#' +#' Delete patterns from a string vector. +#' +#' @param pattern a string containing a [regular expression] +#' @param x a string vector +#' @return a string vector +#' +#' @examples +#' # delete patterns from a string vector +#' \donttest{del_pattern(pattern = "c", c("abc", "acc"))} +#' +#' @export +del_pattern <- function(pattern, x) gsub(pattern, "", x) diff --git a/man/del_pattern.Rd b/man/del_pattern.Rd new file mode 100644 index 0000000..fce4879 --- /dev/null +++ b/man/del_pattern.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/str.R +\name{del_pattern} +\alias{del_pattern} +\title{Delete patterns} +\usage{ +del_pattern(pattern, x) +} +\arguments{ +\item{pattern}{a string containing a \link{regular expression}} + +\item{x}{a string vector} +} +\value{ +a string vector +} +\description{ +Delete patterns from a string vector. +} +\examples{ +# delete patterns from a string vector +\donttest{del_pattern(pattern = "c", c("abc", "acc"))} + +} diff --git a/man/pull_str.Rd b/man/get_pattern.Rd similarity index 57% rename from man/pull_str.Rd rename to man/get_pattern.Rd index 7dcf124..99911fd 100644 --- a/man/pull_str.Rd +++ b/man/get_pattern.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/str.R -\name{pull_str} -\alias{pull_str} -\title{Pull a first string having a specific pattern.} +\name{get_pattern} +\alias{get_pattern} +\title{Get a first pattern} \usage{ -pull_str(pattern, x, ignore.case = TRUE) +get_pattern(pattern, x, ignore.case = TRUE) } \arguments{ \item{pattern}{a string containing a \link{regular expression}} @@ -17,10 +17,10 @@ pull_str(pattern, x, ignore.case = TRUE) a string vector } \description{ -Pull a first string having a specific pattern. +Get a first pattern from a string vector. } \examples{ -# pull a first string having a specific pattern -\donttest{pull_str(pattern = "c", c("a|b|c", "a|c|c"))} +# get a first pattern from a string vector +\donttest{get_pattern(pattern = "c", c("a|b|c", "a|c|c"))} } diff --git a/man/pull_str_all.Rd b/man/get_pattern_all.Rd similarity index 65% rename from man/pull_str_all.Rd rename to man/get_pattern_all.Rd index ddee950..a4f7565 100644 --- a/man/pull_str_all.Rd +++ b/man/get_pattern_all.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/str.R -\name{pull_str_all} -\alias{pull_str_all} -\title{Pull all the strings having a specific pattern.} +\name{get_pattern_all} +\alias{get_pattern_all} +\title{Get all patterns} \usage{ -pull_str_all(pattern, x, collapse = "|", ignore.case = TRUE) +get_pattern_all(pattern, x, collapse = "|", ignore.case = TRUE) } \arguments{ \item{pattern}{a string containing a \link{regular expression}} @@ -21,10 +21,10 @@ pull_str_all(pattern, x, collapse = "|", ignore.case = TRUE) a string vector } \description{ -Pull all the strings having a specific pattern. +Get all patterns from a string vector. } \examples{ -# pull all the strings having a specific pattern -\donttest{pull_str_all(pattern = "c", c("a|b|c", "a|c|c"))} +# get all patterns from a string vector +\donttest{get_pattern_all(pattern = "c", c("a|b|c", "a|c|c"))} }