内容简介:翻译自:https://stackoverflow.com/questions/1660655/how-to-do-erlang-pattern-matching-using-regular-expressions
当我编写用于文本解析的Erlang程序时,我经常遇到我希望使用正则表达式进行模式匹配的情况.
例如,我希望我可以做这样的事情,其中〜是一个“组合”的正则表达式匹配运算符:
my_function(String ~ ["^[A-Za-z]+[A-Za-z0-9]*$"]) ->
....
我知道正则表达式模块(重新),但AFAIK你不能在模式匹配或守卫时调用函数.
此外,我希望匹配字符串可以以不区分大小写的方式完成.这很方便,例如,在解析HTTP标头时,我很乐意做这样的事情,其中“Str~ {Pattern,Options}”的意思是“使用选项选项匹配模式模式”:
handle_accept_language_header(Header ~ {"Accept-Language", [case_insensitive]}) ->
...
两个问题:
>你通常只使用标准的Erlang来处理这个问题?是否有一些机制/编码风格在简洁和易于阅读方面接近于此?
> Erlang有没有工作(EEP?)来解决这个问题?
-module(multire).
-compile(export_all).
multire([],_) ->
nomatch;
multire([RE|RegExps],String) ->
case re:run(String,RE,[{capture,none}]) of
match ->
RE;
nomatch ->
multire(RegExps,String)
end.
test(Foo) ->
test2(multire(["^Hello","world$","^....$"],Foo),Foo).
test2("^Hello",Foo) ->
io:format("~p matched the hello pattern~n",[Foo]);
test2("world$",Foo) ->
io:format("~p matched the world pattern~n",[Foo]);
test2("^....$",Foo) ->
io:format("~p matched the four chars pattern~n",[Foo]);
test2(nomatch,Foo) ->
io:format("~p failed to match~n",[Foo]).
翻译自:https://stackoverflow.com/questions/1660655/how-to-do-erlang-pattern-matching-using-regular-expressions
以上所述就是小编给大家介绍的《正则表达式 – 如何使用正则表达式进行Erlang模式匹配?》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
PHP实战
Dagfinn Reiersol、Marcus Baker、Chris Shiflett / 张颖 等、段大为 审校 / 人民邮电出版社 / 2010-01 / 69.00元
“对于那些想要在PHP方面更进一步的开发者而言,此书必不可少。” ——Gabriel Malkas, Developpez.com “简而言之,这是我所读过的关于面向对象编程和PHP最好的图书。……强烈推荐此书,绝不要错过!” ——Amazon评论 “此书是理论与实践的完美融合,到目前为止,其他任何图书都无法与它相媲美。如果5颗星是满分,它完全值得10颗星!” ——A......一起来看看 《PHP实战》 这本书的介绍吧!
HTML 编码/解码
HTML 编码/解码
MD5 加密
MD5 加密工具