首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

[札记]erlang正则使用

2013-07-08 
[笔记]erlang正则使用20 {ok,MP}re:compile(abc,[caseless]).{ok,{re_pattern,0,0,69,82,67,80,61,0

[笔记]erlang正则使用
20> {ok,MP}=re:compile("abc",[caseless]).{ok,{re_pattern,0,0, <<69,82,67,80,61,0,0,0,1,0,0,0,6,0,0,0,0,0,0,0,97,1,99, ...>>}}

?这边建立一个匹配"abc"的表达式 选项是不区分大小写?

其他的选项还有很多(看不懂啊 看不懂啊...)

?

?

?

run 执行正则匹配

有两个参数和三个参数的形式 三个参数的形式多了一个选项

一些例子: MP是上面的函数产生的:

48> re:run("abcabc",MP,[]).{match,[{0,3}]}49> re:run("abcabc",MP,[{capture,all,list}]).{match,["abc"]}50> re:run("abcabcABC",MP,[{capture,all,list},global]).{match,[["abc"],["abc"],["ABC"]]}

关于 capture的选项 all是所有的 first是不显示子模式(不知道这么说对不对)的 看例子:

?

78> {ok,A}=re:compile("http(s)?",[caseless]).{ok,{re_pattern,1,0,                <<69,82,67,80,74,0,0,0,1,0,0,0,6,0,0,0,1,0,0,0,104,1,                  112,...>>}}79> re:run("Http://www.baidu.com",A,[{capture,all,list}]).{match,["Http"]}80> re:run("Https://www.baidu.com",A,[{capture,all,list}]).{match,["Https","s"]}81> re:run("Https://www.baidu.com",A,[{capture,[1],list}]).{match,["s"]}82> re:run("Https://www.baidu.com",A,[{capture,first,list}]).{match,["Https"]}83> re:run("Https://www.baidu.com",A,[{capture,all_but_first,list}]).{match,["s"]}
?对于子模式还可以用[1],[2]匹配,例子:105> {ok,A}=re:compile("http(s)?://(.*)/(.*)?",[caseless]).{ok,{re_pattern,3,0, <<69,82,67,80,103,0,0,0,1,0,0,0,7,0,0,0,3,0,0,0,104,1, 47,...>>}}106> re:run("http://www.baidu.com/s?wd=erlang&ch=1&tn=98067068_dg",A,[{capture,[2],list}]).{match,["www.baidu.com"]}107> re:run("http://www.baidu.com/s?wd=erlang&ch=1&tn=98067068_dg",A,[{capture,[3],list}]).{match,["s?wd=erlang&ch=1&tn=98067068_dg"]}??55> re:replace("abcabcabc","a","",[{return,list}])."bcabcabc"56> re:replace("abcabcabc","a","",[{return,list},global])."bcbcbc"57> re:replace("abcabcabc","a","").[[]|<<"bcabcabc">>]

?

?

最后是split:

60> re:split("abcabcabc","a").[<<>>,<<"bc">>,<<"bc">>,<<"bc">>]61> re:split("abcabcabc","a",[{return,list}]).[[],"bc","bc","bc"]

?

文档的example多一点就好了...不然看下来真累...

网上找到个个文档 ,不过文档里有错 run中要匹配全部的加上global选项就可以了:

http://veniceweb.googlecode.com/svn/trunk/public/daily_tech_doc/erlang_re_20091201.txt

还有一个资料 例子比较全:

http://zotonic.com/documentation/908/just-enough-zotonic-source-part-3-regular-expressions

热点排行