gleam-lang / gleam-lang/stdlib
Add named capturing groups to gleam/regex.Match
- Dominant language
- Gleam
- Stars
- 710
- Forks
- 225
- Avg merge
- 5h 52m
- Merged PRs (30d)
- 3
Description
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`.
```gleam
import gleam/io
import gleam/regex
pub fn main() {
let assert Ok(regex) = regex.from_string("(?,)")
let content = "Hello, world!"
regex.scan(regex, content)
|> io.debug
}
```
```
[Match(content: ",", submatches: [Some(",")])]
```
I propose `Match` have the following signature:
```gleam
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
```gleam
import gleam/io
import gleam/regex
pub fn main() {
let assert Ok(regex) = regex.from_string("(?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.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.