Book Image

Elixir Cookbook

By : Paulo Pereira
Book Image

Elixir Cookbook

By: Paulo Pereira

Overview of this book

Table of Contents (16 chapters)
Elixir Cookbook
About the Author
About the Reviewers

Using regular expressions

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.


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{}).

Getting ready

Start a new IEx session by entering iex in your command line.

How to do it…

We perform the same operation we did in the Replacing string codepoints with patterns recipe, as follows:

  1. Define a string to operate on:

    iex(1)> my_string = "[email protected] [email protected]"