The “str_match_all” Function in R

  • Package: stringr

  • Purpose: To extract all matched groups from a character vector using regular expressions.

  • General Class: String Manipulation

  • Required Argument(s):

    • string: A character vector where matches will be sought.

    • pattern: A regular expression pattern.

  • Notable Optional Arguments:

    • None

  • Example:

  • # Example usage
    library(stringr)

    # Create a character vector
    text <- c("apple 123", "banana 456", "orange 789")

    # Use str_match_all to extract all numeric digits from each element
    matches_all <- str_match_all(text, "\\d+")

    # Print the list of matches
    print(matches_all)

  • In this example, the str_match_all function from the stringr package is used to extract all numeric digits from each element of the character vector text using a regular expression pattern (\\d+). The resulting list of matches is then printed to the console. Each element of the list corresponds to a character vector of matches for the respective input string.

Previous
Previous

The “str_remove” Function in R

Next
Next

The “str_match” Function in R