elixir - 17 用 sigil 来生成regexp, string

访问量: 297

参考https://elixir-lang.org/getting-started/sigils.html

生成 regexp

iex(8)> regex = ~r/siwei|dashi/
~r/siwei|dashi/
iex(9)> "siwei" =~ regex
true
iex(10)> "si" =~ regex   
false

生成String

> ~s(你好啊,我是个字符串)
"你好啊,我是个字符串"


生成 charlist

iex(12)> ~c(abc egf lalala)
'abc egf lalala'
iex(13)> i ~c(abc egf lalala)
Term
  'abc egf lalala'
Data type
  List
Description
  This is a list of integers that is printed as a sequence of characters
  delimited by single quotes because all the integers in it represent valid
  ASCII characters. Conventionally, such lists of integers are referred to
  as "charlists" (more precisely, a charlist is a list of Unicode codepoints,
  and ASCII is a subset of Unicode).
Raw representation
  [97, 98, 99, 32, 101, 103, 102, 32, 108, 97, 108, 97, 108, 97]
Reference modules
  List
Implemented protocols
  Collectable, Enumerable, IEx.Info, Inspect, List.Chars, String.Chars

生成 word List ,

iex(15)> ~w(apple banana candy) 
["apple", "banana", "candy"]
iex(16)> ~w(apple banana candy)a
[:apple, :banana, :candy]

String interpolation

iex(17)> name = "小明"
"小明"
iex(18)> ~s(你好啊,#{name})
"你好啊,小明"
iex(19)> ~S(你好啊,#{name})
"你好啊,\#{name}"

订阅/RSS Feed

Subscribe

分类/category