Tracks
/
Gleam
Gleam
/
Syllabus
/
Regular Expressions
Re

Regular Expressions in Gleam

4 exercises

About Regular Expressions

Regular expressions in Gleam follow the PCRE specification (Perl Compatible Regular Expressions), similarly to other popular languages like Java, JavaScript, or Ruby.

The gleam/regex module offers functions for working with regular expressions.

Note

This exercise assumes that you already know regular expression syntax, including character classes, quantifiers, groups, and captures.

if you need to refresh your regular expression knowledge, check out one of those sources: Regular-Expressions.info, Rex Egg, RegexOne, Regular Expressions 101, RegExr.

The most common way to create regular expressions is using the regex.from_string function.

let assert Ok(re) = regex.from_string("test")

The regex.from_string function yields an Error if the regular expression syntax is invalid, so a let-assertion has been used here to ensure the regular expression is valid.

The regex.check function can be used to check if a regular expression matches a string.

let assert Ok(re) = regex.from_string("test")

regex.check(re, "this is a test")
// -> True

regex.check(re, "this is too")
// -> False

Captures

If you wish to capture substrings using a regular expression, the regex.scan function can be used to return a list of matches.

let assert Ok(re) = regex.from_string("[oi]n a (\\w+)")
regex.scan(with: re, content: "I am on a boat in a lake.")
// -> [
//   Match(
//     content: "on a boat",
//     submatches: [Some("boat")]
//   ),
//   Match(
//     content: "in a lake",
//     submatches: [Some("lake")]
//   )
// ]

Modifiers

The behaviour of a regular expression can be modified by creating it with the regex.compile function and passing in options.

let options = regex.Options(case_insensitive: True, multi_line: False)
let assert Ok(re) = regex.compile("[A-Z]", with: options)
regex.check(re, "abc123")
// -> True
Edit via GitHub The link opens in a new window or tab

Learn Regular Expressions