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.
本文介绍在Zotonic源代码中如何运用正则表达式进行字符串匹配及分割操作,并通过示例展示了正则表达式的编译与运行过程。
445

被折叠的 条评论
为什么被折叠?



