1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

paiza.ioでelixirAdvent Calendar 2022

Day 6

paiza.ioでelixir その56

Last updated at Posted at 2022-11-09

概要

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" => ""}

成果物

以上。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?