Skip to content

Add named capturing groups to gleam/regex.Match #663

Open
@yoonthegoon

Description

@yoonthegoon

As it is now, you have the ability to write named capturing groups, but those results don't appear in the list of matches returned from regex.scan.

import gleam/io
import gleam/regex

pub fn main() {
  let assert Ok(regex) = regex.from_string("(?<delim>,)")
  let content = "Hello, world!"

  regex.scan(regex, content)
  |> io.debug
}
[Match(content: ",", submatches: [Some(",")])]

I propose Match have the following signature:

pub type Match {
  Match(
    /// The full string of the match.
    content: String,
    /// A `Regex` can have subpatterns, sup-parts that are in parentheses.
    submatches: List(#(String, Option(String))),
  )
}

So now running something like this

import gleam/io
import gleam/regex

pub fn main() {
  let assert Ok(regex) = regex.from_string("(?<type_of>new|old)\\s+(\\w+)")
  let content = "new match_type"

  regex.scan(regex, content)
  |> io.debug
}

would give you this

[Match(content: "new match_type", submatches: [#("type_of", Some("new")), #("2", Some("match_type"))])]

If the concern is this may break existing used regex, possibly a new added option (which will still break existing compiled regex) or a new groups function can be added that returns just a list of tuples of group name and submatches.

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