读书人

Erlang正则表达式的例证

发布时间: 2012-12-25 16:18:28 作者: rapoo

Erlang正则表达式的例子

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

?

27> re:run("E-mail: xyz@pdq.com", "[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-z]{2,3}").{match,[{8,11}]}

Note: DO NOT use this pattern in production. It needs more refinement and much more testing.

What other goodies does re offer?
split(Subject, RE) -> SplitList

and

split(Subject, RE, Options) -> SplitList

Examples:

28> re:split("this/is/my/path","/").[<<"this">>,<<"is">>,<<"my">>,<<"path">>]

If you wish to use a pattern multiple times and boost perfomance, you can compile it with re:compile/1.

Example:

29>  {_, P} = re:compile("[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-z]{2,3}").{ok,{re_pattern,0,0,                <<69,82,67,80,164,0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,0,0,64,                  ...>>}}30> re:run("E-mail: xyz@pdq.com", P).{match,[{8,11}]}

How are regular expressions used in Zotonic source?

For one of many examples, look at ../zotonic/src/markdown/get_url/1.

get_url(String) ->    HTTP_regex = "^(H|h)(T|t)(T|t)(P|p)(S|s)*://",    case re:run(String, HTTP_regex) of        nomatch    -> not_url;        {match, _} -> get_url1(String, [])    end.

读书人网 >编程

热点推荐