Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

Elixir その2 Advent Calendar 2020 Day 12

posted at

updated at


String.replace/3 (Elixir)

この記事は Elixir その2 Advent Calendar 2020 12日目です。



  • を紹介します
  • IExに聞いてみましょう
$ iex

iex> h String.replace/3

           def replace(subject, pattern, replacement, options \\ [])            

Returns a new string created by replacing occurrences of pattern in subject
with replacement.

The subject is always a string.

The pattern may be a string, a regular expression, or a compiled pattern.

The replacement may be a string or a function that receives the matched pattern
and must return the replacement as a string or iodata.

By default it replaces all occurrences but this behaviour can be controlled
through the :global option; see the "Options" section below.

## Options

   :global - (boolean) if true, all occurrences of pattern are replaced
    with replacement, otherwise only the first occurrence is replaced. Defaults
    to true

## Examples

    iex> String.replace("a,b,c", ",", "-")

    iex> String.replace("a,b,c", ",", "-", global: false)

The pattern may also be a list of strings and the replacement may also be a
function that receives the matches:

    iex> String.replace("a,b,c", ["a", "c"], fn <<char>> -> <<char + 1>> end)

When the pattern is a regular expression, one can give \N or \g{N} in the
replacement string to access a specific capture in the regular expression:

    iex> String.replace("a,b,c", ~r/,(.)/, ",\\1\\g{1}")

Notice we had to escape the backslash escape character (i.e., we used \N
instead of just \N to escape the backslash; same thing for \g{N}). By giving
\0, one can inject the whole match in the replacement string.

A compiled pattern can also be given:

    iex> pattern = :binary.compile_pattern(",")
    iex> String.replace("a,b,c", pattern, "[]")

When an empty string is provided as a pattern, the function will treat it as an
implicit empty string between each grapheme and the string will be
interspersed. If an empty string is provided as replacement the subject will be

    iex> String.replace("ELIXIR", "", ".")

    iex> String.replace("ELIXIR", "", "")
  • わかりやすいですね!


  • こんなことがありました
  • markdownで書いた表がズレているのです
  • 56番 :point_down::point_down_tone1::point_down_tone2::point_down_tone3::point_down_tone4::point_down_tone5:
  • 記事のタイトルに|が含まれていて、それが表の区切りと区別がつかないのですね
  • ElixirPipe Operator |> が人気がありますので、|は記事タイトルで使われることはよくありそうです

スクリーンショット 2020-12-12 2.31.32.png

|56|NeosVR+Elixirで気軽にVR WebSocketプログラミング(VR |> AR投影アプリの裏側)<br>@piacerex|2020-12-11|3|
|57|実験: レガシーなImage ProcessingをElixirのパイプで書いてみる<br>@ShozF|2020-12-09|3|


iex> String.replace("NeosVR+Elixirで気軽にVR WebSocketプログラミング(VR |> AR投影アプリの裏側)", "|", "&#124;")
"NeosVR+Elixirで気軽にVR WebSocketプログラミング(VR &#124;> AR投影アプリの裏側)"
  • |&#124;に置きかわっていますね!

スクリーンショット 2020-12-12 2.32.04.png

:tada::tada::tada: (表がいい感じになりましたね!)

Wrapping Up :christmas_tree::santa::santa_tone1::santa_tone2::santa_tone3::santa_tone4::santa_tone5::christmas_tree:

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
What you can do with signing up
Help us understand the problem. What are the problem?