gossamer/regexp_extra
Extras for gleam/regexp — full flag access beyond the two-field
Options record upstream exposes, plus introspection accessors,
named-group scanning, and ES2025 escape.
Types
A match with its named capture groups, extending
gleam/regexp.Match
with a groups map keyed by capture-group name.
pub type Match {
Match(
content: String,
submatches: List(option.Option(String)),
groups: dict.Dict(String, String),
)
}
Constructors
-
Match( content: String, submatches: List(option.Option(String)), groups: dict.Dict(String, String), )Arguments
- content
-
The full string of the match.
- submatches
-
The positional capture groups, as in
gleam/regexp.Match. - groups
-
The named capture groups, keyed by name. A group that did not participate in the match is absent.
A Regexp flag. The full set is reachable via compile and flags;
gleam/regexp.compile’s Options exposes only IgnoreCase and
Multiline.
pub type RegexpFlag {
Global
IgnoreCase
Multiline
DotAll
Unicode
UnicodeSets
Sticky
HasIndices
}
Constructors
-
Globalg— find all matches in a string, not just the first. -
IgnoreCasei— case-insensitive matching. -
Multilinem—^and$match per-line, not per-string. -
DotAlls—.matches newline characters. -
Unicodeu— Unicode mode: surrogate pairs treated as single code points,\u{...}escapes recognized. -
UnicodeSetsv— extended Unicode set features. Mutually exclusive withUnicode. -
Stickyy— anchored matching atlastIndex. -
HasIndicesd— capture-group indices in match results (ES2022).
Values
pub fn compile(
pattern: String,
with flags: List(RegexpFlag),
) -> Result(regexp.Regexp, regexp.CompileError)
Compiles pattern with exactly the flags given. Unlike
gleam/regexp.compile, no flags are auto-added. Returns an error if
pattern is not a valid regular expression or the flag set is invalid
(e.g., Unicode and UnicodeSets together).
Examples
let assert Ok(re) =
regexp_extra.compile("abc", with: [Global, Unicode, Sticky])
regexp.check(with: re, content: "abc")
// -> True
pub fn escape(string: String) -> String
Escapes regex metacharacters in string so it can be embedded as a
literal pattern. Equivalent to JavaScript’s static RegExp.escape
(ES2025).
Examples
let assert Ok(re) =
regexp.from_string(regexp_extra.escape("a.b*c+"))
regexp.check(with: re, content: "a.b*c+")
// -> True
pub fn flags(regexp: regexp.Regexp) -> List(RegexpFlag)
The flags set on regexp, including ones gleam/regexp doesn’t
surface.
Examples
let assert Ok(re) = regexp.from_string("abc")
regexp_extra.flags(re)
// -> [Global, Unicode]
pub fn scan(
with regexp: regexp.Regexp,
content string: String,
) -> List(Match)
Collects all matches of regexp, like
gleam/regexp.scan,
additionally reporting each match’s named capture groups. The
positional submatches still carry every group, named or not, so
reach for the groups map when a pattern uses (?<name>...).
Examples
let assert Ok(re) = regexp.from_string("(?<year>\\d{4})-(?<month>\\d{2})")
let assert [match] = regexp_extra.scan(with: re, content: "2024-05")
dict.get(match.groups, "year")
// -> Ok("2024")
pub fn source(regexp: regexp.Regexp) -> String
The original pattern string the regex was compiled from.
Examples
let assert Ok(re) = regexp.from_string("abc(\\d+)")
regexp_extra.source(re)
// -> "abc(\\d+)"