Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export(str_glue)
export(str_glue_data)
export(str_interp)
export(str_length)
export(str_like)
export(str_locate)
export(str_locate_all)
export(str_match)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

* New `str_split_n()` function to extract only a single piece from a string
(#278, @bfgray3).

* New `str_like()` function which allows the use of SQL wildcards
(#280, @rjpat).

# stringr 1.4.0

Expand Down
38 changes: 38 additions & 0 deletions R/detect.r
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,41 @@ str_ends <- function(string, pattern, negate = FALSE) {
}
)
}

#' Detect the presence of a pattern in the string using SQL LIKE convention.
#'
#' @description
#' Follows the structure of the SQL `LIKE` operator:
#' * Must match the entire string
#' * `_` matches a single character (like `.`)
#' * \verb{\%} matches any number of characters (`.*`)
#' * \verb{\\\%} and `\_` match literal \verb{\%} and `_`
#' * The match is case insentistive by default
#'
#' @inheritParams str_detect
#' @param pattern A charcter vector containing a SQL "like" pattern.
#' See above for details.
#' @param ignore_case Ignore case of matches? Defaults to `TRUE` to match
#' the SQL `LIKE` operator.
#' @return A logical vector.
#' @export
#' @examples
#' fruit <- c("apple", "banana", "pear", "pinapple")
#' str_like(fruit, "app")
#' str_like(fruit, "app%")
#' str_like(fruit, "ba_ana")
#' str_like(fruit, "%APPLE")
str_like <- function(string, pattern, ignore_case = TRUE) {
if (!is.character(pattern) || is.object(pattern)) {
stop("`pattern` must be a character vector", call. = FALSE)
}

pattern <- regex(like_to_regex(pattern), ignore_case = ignore_case)
stri_detect_regex(string, pattern, opts_regex = opts(pattern))
}

like_to_regex <- function(pattern) {
converted <- stri_replace_all_regex(pattern, "(?<!\\\\|\\[)%(?!\\])", "\\.\\*")
converted <- stri_replace_all_regex(converted, "(?<!\\\\|\\[)_(?!\\])", "\\.")
paste0("^", converted, "$")
}
38 changes: 38 additions & 0 deletions man/str_like.Rd

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

17 changes: 17 additions & 0 deletions tests/testthat/test-detect.r
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,20 @@ test_that("str_ends works", {
# Special typing of patterns.
expect_true(str_ends("ab", fixed("B", ignore_case = TRUE)))
})


# str_like ----------------------------------------------------------------

test_that("like_to_regex generates expected regexps",{
expect_equal(like_to_regex("ab%"), "^ab.*$")
expect_equal(like_to_regex("ab_"), "^ab.$")

# escaping
expect_equal(like_to_regex("ab\\%"), "^ab\\%$")
expect_equal(like_to_regex("ab[%]"), "^ab[%]$")
})

test_that("str_like works", {
expect_true(str_like("abc", "ab%"))
expect_error(str_like("abc", regex("x")), "character vector")
})