Regex.split
split
, go back to Regex module for more information.
Specs
Splits the given target based on the given pattern and in the given number of parts.
Options
:parts
- when specified, splits the string into the given number of parts. If not specified,:parts
defaults to:infinity
, which will split the string into the maximum number of parts possible based on the given pattern.:trim
- whentrue
, removes empty strings (""
) from the result. Defaults tofalse
.:on
- specifies which captures to split the string on, and in what order. Defaults to:first
which means captures inside the regex do not affect the splitting process.:include_captures
- whentrue
, includes in the result the matches of the regular expression. The matches are not counted towards the maximum number of parts if combined with the:parts
option. Defaults tofalse
.
Examples
iex> Regex.split(~r{-}, "a-b-c")
["a", "b", "c"]
iex> Regex.split(~r{-}, "a-b-c", parts: 2)
["a", "b-c"]
iex> Regex.split(~r{-}, "abc")
["abc"]
iex> Regex.split(~r{}, "abc")
["", "a", "b", "c", ""]
iex> Regex.split(~r{a(?<second>b)c}, "abc")
["", ""]
iex> Regex.split(~r{a(?<second>b)c}, "abc", on: [:second])
["a", "c"]
iex> Regex.split(~r{(x)}, "Elixir", include_captures: true)
["Eli", "x", "ir"]
iex> Regex.split(~r{a(?<second>b)c}, "abc", on: [:second], include_captures: true)
["a", "b", "c"]