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

New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New