codecamp

Elixir 正则表达式

Elixir中最常用的印记是​~r​,用于创造正则表达式:

# A regular expression that matches strings which contain "foo" or "bar":
iex> regex = ~r/foo|bar/
~r/foo|bar/
iex> "foo" =~ regex
true
iex> "bat" =~ regex
false

Elixir提供Perl兼容的正则表达式(regexes),由PCRE库实现.正则也支持修饰符.例如,修饰符​i​使得正则表达式对大小写不敏感:

iex> "HELLO" =~ ~r/hello/
false
iex> "HELLO" =~ ~r/hello/i
true

查看​Regex​模块以获得更多关于正则表达式的修饰符和操作的信息.

目前,所有的例子都用​/​来包围正则表达式.然而印记支持8种分隔符:

~r/hello/
~r|hello|
~r"hello"
~r'hello'
~r(hello)
~r[hello]
~r{hello}
~r<hello>

支持不同的分隔符的原因是可以更好地适应不同的印记.例如,使用括号作为分隔符可能会和正则中的括号搞混.然而,括号对于其它的印记可能很好用,比如我们将看到的.


Elixir :into选项
Elixir 字符串
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

Elixir 基本操作符

Elixir 二进制,字符串和字符列表

Elixir 类型规格与行为

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }