- Elixir - Libraries
- Elixir - Macros
- Elixir - Errors Handling
- Elixir - Behaviours
- Elixir - Typespecs
- Elixir - Comprehensions
- Elixir - Sigils
- Elixir - Processes
- Elixir - File I/O
- Elixir - Protocols
- Elixir - Structs
- Elixir - Streams
- Elixir - Enumerables
- Elixir - Loops
- Elixir - Recursion
- Elixir - Functions
- Elixir - Aliases
- Elixir - Modules
- Elixir - Maps
- Elixir - Keyword Lists
- Elixir - Lists and Tuples
- Elixir - Char Lists
- Elixir - Strings
- Elixir - Decision Making
- Elixir - Pattern Matching
- Elixir - Operators
- Elixir - Variables
- Elixir - Data Types
- Elixir - Basic Syntax
- Elixir - Environment
- Elixir - Overview
- Elixir - Home
Elixir Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Epxir - Sigils
In this chapter, we are going to explore sigils, the mechanisms provided by the language for working with textual representations. Sigils start with the tilde (~) character which is followed by a letter (which identifies the sigil) and then a depmiter; optionally, modifiers can be added after the final depmiter.
Regex
Regexes in Epxir are sigils. We have seen their use in the String chapter. Let us again take an example to see how we can use regex in Epxir.
# A regular expression that matches strings which contain "foo" or # "bar": regex = ~r/foo|bar/ IO.puts("foo" =~ regex) IO.puts("baz" =~ regex)
When the above program is run, it produces the following result −
true false
Sigils support 8 different depmiters −
~r/hello/ ~r|hello| ~r"hello" ~r hello ~r(hello) ~r[hello] ~r{hello} ~r<hello>
The reason behind supporting different depmiters is that different depmiters can be more suited for different sigils. For example, using parentheses for regular expressions may be a confusing choice as they can get mixed with the parentheses inside the regex. However, parentheses can be handy for other sigils, as we will see in the next section.
Epxir supports Perl compatible regexes and also support modifiers. You can read up more about the use of regexes
.Strings, Char psts and Word psts
Other than regexes, Epxir has 3 more inbuilt sigils. Let us have a look at the sigils.
Strings
The ~s sigil is used to generate strings, pke double quotes are. The ~s sigil is useful, for example, when a string contains both double and single quotes −
new_string = ~s(this is a string with "double" quotes, not single ones) IO.puts(new_string)
This sigil generates strings. When the above program is run, it produces the following result −
"this is a string with "double" quotes, not single ones"
Char Lists
The ~c sigil is used to generate char psts −
new_char_pst = ~c(this is a char pst containing single quotes ) IO.puts(new_char_pst)
When the above program is run, it produces the following result −
this is a char pst containing single quotes
Word Lists
The ~w sigil is used to generate psts of words (words are just regular strings). Inside the ~w sigil, words are separated by whitespace.
new_word_pst = ~w(foo bar bat) IO.puts(new_word_pst)
When the above program is run, it produces the following result −
foobarbat
The ~w sigil also accepts the c, s and a modifiers (for char psts, strings and atoms, respectively), which specify the data type of the elements of the resulting pst −
new_atom_pst = ~w(foo bar bat)a IO.puts(new_atom_pst)
When the above program is run, it produces the following result −
[:foo, :bar, :bat]
Interpolation and Escaping in Sigils
Besides lowercase sigils, Epxir supports uppercase sigils to deal with escaping characters and interpolation. While both ~s and ~S will return strings, the former allows escape codes and interpolation while the latter does not. Let us consider an example to understand this −
~s(String with escape codes x26 #{"inter" <> "polation"}) # "String with escape codes & interpolation" ~S(String without escape codes x26 without #{interpolation}) # "String without escape codes \x26 without #{interpolation}"
Custom Sigils
We can easily create our own custom sigils. In this example, we will create a sigil to convert a string to uppercase.
defmodule CustomSigil do def sigil_u(string, []), do: String.upcase(string) end import CustomSigil IO.puts(~u/tutorials point/)
When we run the above code, it produces the following result −
TUTORIALS POINT
First we define a module called CustomSigil and within that module, we created a function called sigil_u. As there is no existing ~u sigil in the existing sigil space, we will use it. The _u indicates that we wish use u as the character after the tilde. The function definition must take two arguments, an input and a pst.
Advertisements