aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/gleam/regex.gleam53
-rw-r--r--src/gleam_stdlib.erl26
2 files changed, 78 insertions, 1 deletions
diff --git a/src/gleam/regex.gleam b/src/gleam/regex.gleam
index 3740984..3f49eb1 100644
--- a/src/gleam/regex.gleam
+++ b/src/gleam/regex.gleam
@@ -3,8 +3,26 @@
//// all of the PCRE library is interfaced and some parts of the library go beyond
//// what PCRE offers. Currently PCRE version 8.40 (release date 2017-01-11) is used.
+import gleam/option.{Option}
+
pub external type Regex
+/// The details about a particular match:
+///
+/// - match — the full string of the match.
+/// - index — the index of the match in the original string.
+/// - number — each match is numbered, starting from 1
+/// - submatches — a Regex can have subpatterns, sup-parts that are in parentheses.
+///
+pub type Match {
+ Match(
+ match: String,
+ index: Int,
+ number: Int,
+ submatches: List(Option(String)),
+ )
+}
+
/// When a regular expression fails to compile:
///
/// - error — a descriptive error message
@@ -73,3 +91,38 @@ pub external fn from_string_with(
///
pub external fn match(Regex, String) -> Bool =
"gleam_stdlib" "regex_match"
+
+/// Split a string
+///
+/// ## Examples
+///
+/// > let Ok(re) = from_string(" *, *")
+/// > match(re, "foo,32, 4, 9 ,0")
+/// ["foo", "32", "4", "9", "0"]
+///
+pub external fn split(Regex, String) -> List(String) =
+ "gleam_stdlib" "regex_split"
+
+/// Collects all matches of the regular expression.
+///
+/// ## Examples
+///
+/// > let Ok(re) = regex.from_string("[oi]n a (\\w+)")
+/// > regex.scan(re, "I am on a boat in a lake.")
+/// [
+/// Match(
+/// match: "on a boat",
+/// index: 5,
+/// number: 1,
+/// submatches: [Some("boat")]
+/// ),
+/// Match(
+/// match: "in a lake",
+/// index: 15,
+/// number: 2,
+/// submatches: [Some("lake")]
+/// )
+/// ]
+///
+pub external fn scan(Regex, String) -> List(Match) =
+ "gleam_stdlib" "regex_scan"
diff --git a/src/gleam_stdlib.erl b/src/gleam_stdlib.erl
index 47fbdb5..e89458b 100644
--- a/src/gleam_stdlib.erl
+++ b/src/gleam_stdlib.erl
@@ -11,7 +11,7 @@
string_pad/4, decode_tuple2/1, decode_map/1, bit_string_int_to_u32/1,
bit_string_int_from_u32/1, bit_string_append/2, bit_string_part_/3,
decode_bit_string/1, regex_from_string/1, regex_from_string_with/2,
- regex_match/2, base_decoded4/1]).
+ regex_match/2, regex_split/2, regex_scan/2, base_decoded4/1]).
should_equal(Actual, Expected) -> ?assertEqual(Expected, Actual).
should_not_equal(Actual, Expected) -> ?assertNotEqual(Expected, Actual).
@@ -190,6 +190,30 @@ regex_match(Regex, String) ->
_ -> false
end.
+regex_split(Regex, String) ->
+ re:split(String, Regex).
+
+regex_submatches(String, {S, L}) ->
+ SubMatch = string:slice(String, S, L),
+ case string:is_empty(SubMatch) of
+ true -> none;
+ false -> {some, SubMatch}
+ end.
+
+regex_matches(String, [{S, L} | Submatches], Number) ->
+ {match, string:slice(String, S, L), S, Number,
+ lists:map(fun(X) -> regex_submatches(String, X) end, Submatches)}.
+
+regex_captured(_, [], _) -> [];
+regex_captured(String, [ H | T ], Number) ->
+ [ regex_matches(String, H, Number) | regex_captured(String, T, Number + 1) ].
+
+regex_scan(Regex, String) ->
+ case re:run(String, Regex, [global]) of
+ {match, Captured} -> regex_captured(String, Captured, 1);
+ _ -> []
+ end.
+
base_decoded4(S) ->
try {ok, base64:decode(S)} catch
error:badarith -> {error, nil}