str_like: Detect a pattern in the same way as 'SQL"s 'LIKE' operator

View source: R/detect.R

str_likeR Documentation

Detect a pattern in the same way as SQL's LIKE operator

Description

str_like() follows the conventions of the SQL LIKE operator:

  • Must match the entire string.

  • ⁠_⁠ matches a single character (like .).

  • ⁠%⁠ matches any number of characters (like ⁠.*⁠).

  • ⁠\%⁠ and ⁠\_⁠ match literal ⁠%⁠ and ⁠_⁠.

  • The match is case insensitive by default.

Usage

str_like(string, pattern, ignore_case = TRUE)

Arguments

string

Input vector. Either a character vector, or something coercible to one.

pattern

A character vector containing a SQL "like" pattern. See above for details.

ignore_case

Ignore case of matches? Defaults to TRUE to match the SQL LIKE operator.

Value

A logical vector the same length as string.

Examples

fruit <- c("apple", "banana", "pear", "pineapple")
str_like(fruit, "app")
str_like(fruit, "app%")
str_like(fruit, "ba_ana")
str_like(fruit, "%APPLE")

tidyverse/stringr documentation built on Nov. 20, 2023, 5:16 p.m.