概要
paiza.ioでelixirやってみた。
Regex.scan使ってみた。
サンプルコード
Regex.scan(~r/foo([0-9])/, "foo1 foo2")
|> IO.inspect
Regex.scan(~r/c(d|e)/, "abcd abce")
|> IO.inspect
Regex.scan(~r{o+}i, "foo fooooo!")
|> IO.inspect
Regex.scan(~r{o+}i, "foo fooooo!", return: :index)
|> IO.inspect
Regex.scan(~r/c(?:d|e)/, "abcd abce")
|> IO.inspect
html = "<html><span>ok</span></html>"
Regex.scan(~r/(.*?>)([^&<]+)(<\/span>)/s, html, capture: :all_but_first)
|> Enum.map_reduce(0, fn [l, c, r], acc ->
c = c
|> String.split()
|> Enum.with_index()
|> Enum.map(fn {char, index} ->
"<span id='CHAR-#{index + acc}'>#{char}</span>"
end)
{[l, c, r], acc + length(c)}
end)
|> elem(0)
|> IO.puts()
Regex.scan(~r/A(.+?)C/, "A0bCA1bCA2bCA3bCA4bC")
|> IO.inspect
|> Enum.map(fn [_, v] ->
IO.puts(v)
end)
実行結果
[["foo1", "1"], ["foo2", "2"]]
[["cd", "d"], ["ce", "e"]]
[["oo"], ["ooooo"]]
[[{1, 2}], [{5, 5}]]
[["cd"], ["ce"]]
<html><span><span id='CHAR-0'>ok</span></span>
[["A0bC", "0b"], ["A1bC", "1b"], ["A2bC", "2b"], ["A3bC", "3b"], ["A4bC", "4b"]]
0b
1b
2b
3b
4b
成果物
以上。