The submatches can't be a tuple because a tuple is not a single type. For
instance, (String, String) is a different type than (String, String,
String). In Elm, the type of the return value can't depend on the value of
the input.
if you wanted to get the first and second match out, you could do something
like
case submatches of
first :: second :: _ ->
(Maybe.withDefault "" first, Maybe.withDefault "" second)
_ ->
("", "")
And if you want to treat submatches as an array, all you have to do is
call "Array.fromList
submatches"
On Wed, Jul 20, 2016 at 3:50 PM, Duane Johnson <[email protected]>
wrote:
> I have a regex match whose result is guaranteed to have two submatches, if
> any match at all:
>
> ```
> import Regex exposing (HowMany(All), regex, find)
> find All (regex "(\\d+)?([A-Z])") someString
> ```
>
> the result looks like this when someString is "HE2LO":
>
> ```
> [{ match = "H", submatches = [Nothing,Just "H"], index = 0, number = 1 }
> ,{ match = "E", submatches = [Nothing,Just "E"], index = 1, number = 2 }
> ,{ match = "2L", submatches = [Just "2",Just "L"], index = 2, number = 3 }
> ,{ match = "O", submatches = [Nothing,Just "O"], index = 4, number = 4 }
> ]
> ```
>
> Why are the submatches of type List rather than Tuple? At the very least,
> shouldn't it be an array so we can access the submatches by index?
>
> Instead, it seems we need to deconstruct the submatch list in an awkward
> way, something like:
>
> ```
> -- for instance, let's say submatches = [Nothing,Just "H"]
> pair =
> (submatches |> |> List.head |> Maybe.withDefault Nothing |>
> Maybe.withDefault ""
> ,submatches |> List.tail |> Maybe.withDefault [Nothing] |> List.head |>
> Maybe.withDefault Nothing |> Maybe.withDefault ""
> )
> ```
>
> Is there a better way to get the first and second match out?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Elm Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups "Elm
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.