Skip to content

Commit

Permalink
pattern functions are added
Browse files Browse the repository at this point in the history
  • Loading branch information
seokhoonj committed Mar 14, 2024
1 parent 6bb3400 commit b61be12
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 27 deletions.
5 changes: 3 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
37 changes: 26 additions & 11 deletions R/str.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
24 changes: 24 additions & 0 deletions man/del_pattern.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions man/pull_str.Rd → man/get_pattern.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions man/pull_str_all.Rd → man/get_pattern_all.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b61be12

Please sign in to comment.