简体   繁体   English

什么是灵丹妙药中的=〜运算符

[英]What is =~ operator in elixir

From the documentation I understand how =~ operator works to match regex, but I don't understand the general use of this operator. 文档我理解=~运算符如何匹配正则表达式,但我不明白这个运算符的一般用途。

For example, what does "foo" =~ "foo" mean? 例如, "foo" =~ "foo"是什么意思? How is it different from "foo" == "foo" ? 它与"foo" == "foo"有什么不同?

It's not documented on that page, but it's documented in Kernel.=~/2 that when the RHS is a string, =~ checks if LHS contains RHS: 它没有在该页面上记录,但它在Kernel.=~/2 ,当RHS是一个字符串时, =~检查LHS是否包含RHS:

iex(1)> "foo" =~ "f"
true
iex(2)> "foo" =~ "o"
true

It does not implicitly convert RHS to regex: 它不会隐式地将RHS转换为正则表达式:

iex(3)> "foo" =~ "."
false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM