概要
paiza.ioでelixirやってみた。
Regex.named_captures使ってみた。
サンプルコード
Regex.named_captures(~r/A(?<hoge>.+)C/, "A0bCA1bCA2bCA3bCA4bC")
|> IO.inspect
string = "Here is my phone number: 999-111-1234"
regex = ~r/number: (?<phone>[\d|-]+)/
Regex.named_captures(regex, string)
|> IO.inspect
regex = ~r"\[tag1\](.+?)\[\/tag1\]"
Regex.run(regex, "[tag1]bar[/tag1]")
|> IO.inspect
Regex.scan(regex, "[tag1]bar[/tag1] [tag1]baz[/tag1]")
|> IO.inspect
Regex.named_captures(~r/A(?<hoge>.*)C/, "A0bCA1bCA2bCA3bCA4bC")
|> IO.inspect
Regex.named_captures(~r/(?<name>\w)/, "ab")
Regex.scan(~r/\w/, "ab")
|> Enum.map(fn x ->
%{name: hd(x)}
end)
|> IO.inspect
Regex.scan(~r/\w/, "ab")
|> List.flatten
|> IO.inspect
Regex.named_captures(~r{(?<protocol>http://|https://)(?<domain>.*)}, "https://qiita.com")
|> IO.inspect
Regex.named_captures(~r[Guard #(?<guard_id>\d+) begins shift], "[1518-11-01 00:00] Guard #10 begins shift")
|> IO.inspect
Regex.named_captures(~r/(?<minus>-?)(?<digit>[[:digit:]]*)(?<frac>\.?[[:digit:]]*)(?<exp>[eE]?[-+]?[[:digit:]]*)(?<rest>.*)/s, "12345")
|> IO.inspect
Regex.named_captures(~r/(?<minus>-?)(?<digit>[[:digit:]]*)(?<frac>\.?[[:digit:]]*)(?<exp>[eE]?[-+]?[[:digit:]]*)(?<rest>.*)/s, "12:45")
|> IO.inspect
Regex.named_captures(~r/(?<minus>-?)(?<digit>[[:digit:]]*)(?<frac>\.?[[:digit:]]*)(?<exp>[eE]?[-+]?[[:digit:]]*)(?<rest>.*)/s, "12.45")
|> IO.inspect
Regex.named_captures(~r/(?<minus>-?)(?<digit>[[:digit:]]*)(?<frac>\.?[[:digit:]]*)(?<exp>[eE]?[-+]?[[:digit:]]*)(?<rest>.*)/s, "1.2e+4")
|> IO.inspect
実行結果
%{"hoge" => "0bCA1bCA2bCA3bCA4b"}
%{"phone" => "999-111-1234"}
["[tag1]bar[/tag1]", "bar"]
[["[tag1]bar[/tag1]", "bar"], ["[tag1]baz[/tag1]", "baz"]]
%{"hoge" => "0bCA1bCA2bCA3bCA4b"}
[%{name: "a"}, %{name: "b"}]
["a", "b"]
%{"domain" => "qiita.com", "protocol" => "https://"}
%{"guard_id" => "10"}
%{"digit" => "12345", "exp" => "", "frac" => "", "minus" => "", "rest" => ""}
%{"digit" => "12", "exp" => "", "frac" => "", "minus" => "", "rest" => ":45"}
%{"digit" => "12", "exp" => "", "frac" => ".45", "minus" => "", "rest" => ""}
%{"digit" => "1", "exp" => "e+4", "frac" => ".2", "minus" => "", "rest" => ""}
成果物
以上。