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

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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
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
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
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
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
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

We're in Beta

About us Mission Statement