Today I Learned

hashrocket A Hashrocket project

Named Captures with Elixir Regular Expressions

I can't believe I'm just now learning about this! How neat!

iex(0)> string = "Here is my phone number: 999-111-1234"
"Here is my phone number: 999-111-1234"
iex(1)> regex = ~r/number: (?<phone>[\d|-]+)/
~r/number: (?<phone>[\d|-]+)/
iex(2)> Regex.named_captures(regex, string)
%{"phone" => "999-111-1234"}

Write a regex capture with ?<> in the beginning and get back a map of your captures with Regex.named_captures/2

See More #elixir TILs
Looking for help? At Hashrocket, we 💜 Elixir! From our many Elixir client projects, to sponsoring the Chicago Elixir Meetup, to the source code for this application, we are invested in this community. Contact us today to talk about your Elixir project.