Ajwah

Ajwah

Regex Multi Capture Groups

Regex Question: Given following string: "default via 10.141.23.1 dev eth0 \n10.141.23.0/24 dev eth0 proto kernel scope link src 10.141.23.235 \n169.254.169.254 dev eth0 \n172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 \n"

I want to retrieve the following result: %{"default" => "10.141.23.1", "docker0" => "172.17.0.1"}

I can retrieve separately, like this: ~r/default via (?<default>\d+\.\d+\.\d+\.\d+)/ |> Regex.named_captures(str)

and ~r/dev docker0 proto kernel scope link src (?<docker0>\d+\.\d+\.\d+\.\d+)/ |> Regex.named_captures(str)
But I am not able to retrieve the desired result by combining both into one regex

e.g.

~r/default via (?<default>\d+\.\d+\.\d+\.\d+).*dev docker0 proto kernel scope link src (?<docker0>\d+\.\d+\.\d+\.\d+)/ |> Regex.named_captures(str)

returns nil

Most Liked

Qqwy

Qqwy

TypeCheck Core Team

It is because by default, regular expressions treat newline characters in a special way. In your case, you’ll have to add the dotall option (sigil flag s) to ensure that the .* part between your two regular expression parts will not fail when a \n is reached:

r6 = ~r{default via (?<default>\d+\.\d+\.\d+\.\d+).+dev docker0 proto kernel scope link src (?<docker0>\d+\.\d+\.\d+\.\d+)}s
Regex.named_captures(r6, str)

Where Next?

Popular in Questions Top

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
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
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
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
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
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

Other popular topics Top

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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement