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

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
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
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
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
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
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 records...
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
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

Other popular topics Top

stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52774 488
New
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

We're in Beta

About us Mission Statement