diff options
author | Giacomo Cavalieri <giacomo.cavalieri@icloud.com> | 2023-12-31 22:28:44 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2024-01-02 17:08:28 +0000 |
commit | bcc728b663df5261b7e73248b34522678d0db7fe (patch) | |
tree | 93b8270e68e789c6b0e4d42ff0bce266de58bd86 /src | |
parent | 2eaaf7d4191666b3fc943fe6a599eacffaae2588 (diff) | |
download | gleam_stdlib-bcc728b663df5261b7e73248b34522678d0db7fe.tar.gz gleam_stdlib-bcc728b663df5261b7e73248b34522678d0db7fe.zip |
regex doc examples
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/regex.gleam | 137 |
1 files changed, 62 insertions, 75 deletions
diff --git a/src/gleam/regex.gleam b/src/gleam/regex.gleam index 9ffda78..b72a02e 100644 --- a/src/gleam/regex.gleam +++ b/src/gleam/regex.gleam @@ -39,17 +39,17 @@ pub type Options { /// ## Examples /// /// ```gleam -/// > let options = Options(case_insensitive: False, multi_line: True) -/// > let assert Ok(re) = compile("^[0-9]", with: options) -/// > check(re, "abc\n123") -/// True +/// let options = Options(case_insensitive: False, multi_line: True) +/// let assert Ok(re) = compile("^[0-9]", with: options) +/// check(re, "abc\n123") +/// // -> True /// ``` /// /// ```gleam -/// > let options = Options(case_insensitive: True, multi_line: False) -/// > let assert Ok(re) = compile("[A-Z]", with: options) -/// > check(re, "abc123") -/// True +/// let options = Options(case_insensitive: True, multi_line: False) +/// let assert Ok(re) = compile("[A-Z]", with: options) +/// check(re, "abc123") +/// // -> True /// ``` /// pub fn compile( @@ -68,24 +68,22 @@ fn do_compile(a: String, with with: Options) -> Result(Regex, CompileError) /// ## Examples /// /// ```gleam -/// > let assert Ok(re) = from_string("[0-9]") -/// > check(re, "abc123") -/// True +/// let assert Ok(re) = from_string("[0-9]") +/// check(re, "abc123") +/// // -> True /// ``` /// /// ```gleam -/// > check(re, "abcxyz") -/// False +/// check(re, "abcxyz") +/// // -> False /// ``` /// /// ```gleam -/// > from_string("[0-9") -/// Error( -/// CompileError( -/// error: "missing terminating ] for character class", -/// byte_index: 4 -/// ) -/// ) +/// from_string("[0-9") +/// // -> Error(CompileError( +/// // error: "missing terminating ] for character class", +/// // byte_index: 4 +/// // )) /// ``` /// pub fn from_string(pattern: String) -> Result(Regex, CompileError) { @@ -97,14 +95,14 @@ pub fn from_string(pattern: String) -> Result(Regex, CompileError) { /// ## Examples /// /// ```gleam -/// > let assert Ok(re) = from_string("^f.o.?") -/// > check(with: re, content: "foo") -/// True +/// let assert Ok(re) = from_string("^f.o.?") +/// check(with: re, content: "foo") +/// // -> True /// ``` /// /// ```gleam -/// > check(with: re, content: "boo") -/// False +/// check(with: re, content: "boo") +/// // -> False /// ``` /// pub fn check(with regex: Regex, content content: String) -> Bool { @@ -120,9 +118,9 @@ fn do_check(a: Regex, b: String) -> Bool /// ## Examples /// /// ```gleam -/// > let assert Ok(re) = from_string(" *, *") -/// > split(with: re, content: "foo,32, 4, 9 ,0") -/// ["foo", "32", "4", "9", "0"] +/// let assert Ok(re) = from_string(" *, *") +/// split(with: re, content: "foo,32, 4, 9 ,0") +/// // -> ["foo", "32", "4", "9", "0"] /// ``` /// pub fn split(with regex: Regex, content string: String) -> List(String) { @@ -147,62 +145,51 @@ fn js_split(a: String, b: Regex) -> List(String) /// ## Examples /// /// ```gleam -/// > let assert Ok(re) = from_string("[oi]n a (\\w+)") -/// > 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")] -/// ) -/// ] +/// let assert Ok(re) = from_string("[oi]n a (\\w+)") +/// 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")]), +/// // ] /// ``` /// /// ```gleam -/// > let assert Ok(re) = regex.from_string("([+|\\-])?(\\d+)(\\w+)?") -/// > scan(with: re, content: "-36") -/// [ -/// Match( -/// content: "-36", -/// submatches: [Some("-"), Some("36")] -/// ) -/// ] -/// -/// > scan(with: re, content: "36") -/// [ -/// Match( -/// content: "36", -/// submatches: [None, Some("36")] -/// ) -/// ] +/// let assert Ok(re) = regex.from_string("([+|\\-])?(\\d+)(\\w+)?") +/// scan(with: re, content: "-36") +/// // -> [ +/// // Match(content: "-36", submatches: [Some("-"), Some("36")]) +/// // ] +/// +/// scan(with: re, content: "36") +/// // -> [ +/// // Match(content: "36", submatches: [None, Some("36")]) +/// // ] /// ``` /// /// ```gleam -/// > let assert Ok(re) = regex.from_string("var\\s*(\\w+)\\s*(int|string)?\\s*=\\s*(.*)") -/// > scan(with: re, content: "var age = 32") -/// [ -/// Match( -/// content: "var age = 32", -/// submatches: [Some("age"), None, Some("32")] -/// ) -/// ] +/// let assert Ok(re) = +/// regex.from_string("var\\s*(\\w+)\\s*(int|string)?\\s*=\\s*(.*)") +/// scan(with: re, content: "var age = 32") +/// // -> [ +/// // Match( +/// // content: "var age = 32", +/// // submatches: [Some("age"), None, Some("32")], +/// // ), +/// // ] /// ``` /// /// ```gleam -/// > let assert Ok(re) = regex.from_string("let (\\w+) = (\\w+)") -/// > scan(with: re, content: "let age = 32") -/// [ -/// Match( -/// content: "let age = 32", -/// submatches: [Some("age"), Some("32")] -/// ) -/// ] -/// -/// > scan(with: re, content: "const age = 32") -/// [] +/// let assert Ok(re) = regex.from_string("let (\\w+) = (\\w+)") +/// scan(with: re, content: "let age = 32") +/// // -> [ +/// // Match( +/// // content: "let age = 32", +/// // submatches: [Some("age"), Some("32")], +/// // ), +/// // ] +/// +/// scan(with: re, content: "const age = 32") +/// // -> [] /// ``` /// pub fn scan(with regex: Regex, content string: String) -> List(Match) { |