The “str_match” Function in R

  • Package: stringr

  • Purpose: To extract 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 to extract numeric digits from each element
    matches <- str_match(text, "\\d+")

    # Print the matches
    print(matches)

  • In this example, the str_match function from the stringr package is used to extract numeric digits from each element of the character vector text using a regular expression pattern (\\d+). The resulting matrix of matches is then printed to the console.

Previous
Previous

The “str_match_all” Function in R

Next
Next

The “str_glue” Function in R