aifrak

aifrak

Is it normal that `Regex.compile/2` does not print regex modifiers when atoms are passed in `options`?

Hello,

I have a question regarding the result returns by Regex.compile/2 and Regex.compile!/2.

The Elixir documentation for Regex.compile/2 and Regex.compile!/2 mentions that the second argument is binary() | [term()].

It means modifiers can be added this way:

iex)> {:ok, regex} = Regex.compile("foo", "i")
{:ok, ~r/foo/i}
iex> String.match?("FOO", regex)             
true

But I saw a solution from exercism where regex modifiers can also be added this way:

iex> {:ok, regex} = Regex.compile("foo", [:caseless])
{:ok, ~r/foo/}
iex> String.match?("FOO", regex)                     
true

In the second code snippet, is it normal that the regex is printed without any regex modifiers? ({:ok, ~r/foo/} instead of {:ok, ~r/foo/i})

Marked As Solved

aifrak

aifrak

After checking the answers from @kip and @czrpb, I looked at the code on the main branch and I have noticed that @sabiwara made a PR related to my post.

@kip, @czrpb and @sabiwara, thank you!

Also Liked

kip

kip

ex_cldr Core Team

If you examine the Regex struct for these two examples you see:

iex(1)> IO.inspect Regex.compile!("foo", "i"), structs: false                          
%{
  __struct__: Regex,
  opts: "i",
  re_pattern: {:re_pattern, 0, 0, 0,
   <<69, 82, 67, 80, 77, 0, 0, 0, 1, 0, 0, 0, 241, 0, 0, 0, 255, 255, 255, 255,
     255, 255, 255, 255, 102, 0, 111, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0,
     0, ...>>},
  re_version: {"8.44 2020-02-12", :little},
  source: "foo"
}
~r/foo/i
iex(2)> IO.inspect Regex.compile!("foo", [:caseless]), structs: false
%{
  __struct__: Regex,
  opts: "",
  re_pattern: {:re_pattern, 0, 0, 0,
   <<69, 82, 67, 80, 77, 0, 0, 0, 1, 0, 0, 0, 241, 0, 0, 0, 255, 255, 255, 255,
     255, 255, 255, 255, 102, 0, 111, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0,
     0, ...>>},
  re_version: {"8.44 2020-02-12", :little},
  source: "foo"
}
~r/foo/

And its the :opts field that is used for the Inspect protocol to present the regex.

The reason you don’t see the flags in the second example is that Regex.compile/2 will call Regex.translate_options/2 to translate the string options to the list form. But there is no function to translate them back to the string form, and its the string form that is used for display purposes.

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Other popular topics Top

AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement