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で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

成果物

以上。

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?