sashang

sashang

Interpolating regex expressions

How do I interpolate a regex inside another regex. Not sure if interpolate is the right word so here’s an example:

r = ~r/something/
r1 = ~r/prefix:(r)*/

so r in r1 is not meant to be literally interpreted as ‘r’ but rather the contents of the regex refereced by r.

Marked As Solved

Also Liked

NobbZ

NobbZ

No need to do a Regex.compile/2, one can still use Sigils:

iex(1)> r = "foo"
"foo"
iex(2)> re = ~r/#{r}/
~r/foo/

This way one doesn’t need to remember escaping some Regex stuff which one had to do when using Regex.compile/2. Also it doesn’t read that clunky in the source :wink:

NobbZ

NobbZ

Yeah. I missed the fact that Regex.compile/2 returns a tuple which indicates correctness of the regex while using the small-r sigil is like using a banged version which might crash.

Therefore let me summarize, that one can choose among different possibilities to achieve what was asked for, depending on ones usecase:

Eiji

Eiji

@NobbZ: Yeah, but this method returns what’s wromg with regex, so I think that it’s more safe. Regex module has also method to escape special characters.

Last Post!

NobbZ

NobbZ

Yeah. I missed the fact that Regex.compile/2 returns a tuple which indicates correctness of the regex while using the small-r sigil is like using a banged version which might crash.

Therefore let me summarize, that one can choose among different possibilities to achieve what was asked for, depending on ones usecase:

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31525 112
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New

We're in Beta

About us Mission Statement