Skip to content

Feature Request: str_extract() by Name of group As Well as by Position #603

Description

@GregYannes

Currently we can extract (say) a character matrix via capturing groups, using an integer vector to reference groups by position.

library(stringr)

shopping_list <- c("apples x4", "bag of flour", "bag of sugar", "milk x2")
named_regex <- "(?<g1>[a-z]+) of (?<g2>[a-z]+)"

str_extract(shopping_list, named_regex, group = 1:2)
#>      g1    g2     
#> [1,] NA    NA     
#> [2,] "bag" "flour"
#> [3,] "bag" "sugar"
#> [4,] NA    NA

Created on 2026-05-15 with reprex v2.1.1

It would be nice to reference groups by name, using a character vector.

str_extract(shopping_list, named_regex, group = c("g1", "g2"))

Mixing the two is probably out of scope.

str_extract(shopping_list, named_regex, group = list(1, "g2"))

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions