Elixir supports regular expressions via Erlang's re
module. This is one of those situations that we mentioned in the Using Erlang from Elixir recipe in Chapter 1, Command Line. However, we don't have to use the re
Erlang module directly! We have the Elixir Regex
module that is built on top of the Erlang module and is also based on Perl Compatible Regular Expressions (PCRE).
In this recipe, we use the ~r
sigil to define regular expressions and operate on strings.
Note
The ~r
sigil is a special form that allows for the creation of regular expressions as alternatives to the Regex.compile!/2
function.
Internally, a regular expression is represented by the Regex struct (%Regex{}
).
We perform the same operation we did in the Replacing string codepoints with patterns recipe, as follows:
Define a string to operate on:
iex(1)> my_string = "[email protected] [email protected]" "user1@server...