str_locate: Locate the position of patterns in a string

View source: R/locate.R

str_locateR Documentation

Locate the position of patterns in a string

Description

Vectorised over string and pattern, though using vectorised patterns is relatively slow compared to stringr. If the match is of length 0, (e.g. from a special match like $) end will be one character less than start.

Usage

str_locate(string, pattern)

str_locate_all(string, pattern)

Arguments

string

character vector of strings.

pattern

string or character vector, pattern(s) to match. Can be:

  • A Perl-compatible regular expression (default).

  • Wrap with perl(ignore_case = TRUE) to use case-insensitive matching.

  • Wrap with fixed() to use a fixed/literal match.

  • Wrap with regex() to use a POSIX 1003.2 extended regular expression.

  • Wrap with regex(ignore_case = TRUE) to use case-insensitive matching with a POSIX 1003.2 extended regular expression.

Value

For str_locate(), an integer matrix. First column gives start postion of match, and second column gives end position. For str_locate_all() a list of integer matrices.

See Also

str_extract() for a convenient way of extracting matches.

Examples

fruit <- c("apple", "banana", "pear", "pineapple")
str_locate(fruit, "$")
str_locate(fruit, "a")
str_locate(fruit, "e")
str_locate("apple", c("a", "b", "p", "p"))

str_locate_all(fruit, "a")
str_locate_all(fruit, "e")

# Find location of every character
str_locate_all(fruit, "")

csdaw/stringrb documentation built on Aug. 13, 2022, 10:55 p.m.