str_match

0th

Percentile

Extract matched groups from a string.

Vectorised over string and pattern.

Usage
str_match(string, pattern)

str_match_all(string, pattern)

Arguments
string

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

pattern

Pattern to look for, as defined by an ICU regular expression. See stringi::stringi-search-regex for more details.

Value

For str_match, a character matrix. First column is the complete match, followed by one column for each capture group. For str_match_all, a list of character matrices.

See Also

str_extract() to extract the complete match, stringi::stri_match() for the underlying implementation.

Aliases
  • str_match
  • str_match_all
Examples
# NOT RUN {
strings <- c(" 219 733 8965", "329-293-8753 ", "banana", "595 794 7569",
  "387 287 6718", "apple", "233.398.9187  ", "482 952 3315",
  "239 923 8115 and 842 566 4692", "Work: 579-499-7527", "$1000",
  "Home: 543.355.3679")
phone <- "([2-9][0-9]{2})[- .]([0-9]{3})[- .]([0-9]{4})"

str_extract(strings, phone)
str_match(strings, phone)

# Extract/match all
str_extract_all(strings, phone)
str_match_all(strings, phone)

x <- c("<a> <b>", "<a> <>", "<a>", "", NA)
str_match(x, "<(.*?)> <(.*?)>")
str_match_all(x, "<(.*?)>")

str_extract(x, "<.*?>")
str_extract_all(x, "<.*?>")
# }
Documentation reproduced from package stringr, version 1.4.0, License: GPL-2 | file LICENSE

Community examples

doracheng@maxchip.com.tw at Nov 5, 2018 stringr v1.3.1

strings <- c(" 219 733 8965", "329-293-8753 ", "banana", "595 794 7569", "387 287 6718", "apple", "233.398.9187 ", "482 952 3315", "239 923 8115 and 842 566 4692", "Work: 579-499-7527", "$1000", "Home: 543.355.3679") phone <- "([2-9][0-9]{2})[- .]([0-9]{3})[- .]([0-9]{4})" str_extract(strings, phone) str_match(strings, phone)

doracheng@maxchip.com.tw at Nov 5, 2018 stringr v1.3.1

str_match("2PIN Yield: 0.9933,1,1,0.9933,1,1,1,1", "2PIN Yield: (.+?),")

doracheng@maxchip.com.tw at Nov 5, 2018 stringr v1.3.1

str_match("2PIN Yield: 0.9933,1,1,0.9933,1,1,1,1","2PIN Yield: (.+?),")[,2])

doracheng@maxchip.com.tw at Nov 5, 2018 stringr v1.3.1

strings <- c(" 219 733 8965", "329-293-8753 ", "banana", "595 794 7569", "387 287 6718", "apple", "233.398.9187 ", "482 952 3315", "239 923 8115 and 842 566 4692", "Work: 579-499-7527", "$1000", "Home: 543.355.3679") phone <- "([2-9][0-9]{2})[- .]([0-9]{3})[- .]([0-9]{4})" str_extract(strings, phone) str_match(strings, phone)