7

当我编写进行文本解析的 Erlang 程序时,我经常遇到我喜欢使用正则表达式进行模式匹配的情况。

例如,我希望我能做这样的事情,其中​​ ~ 是一个“组成”的正则表达式匹配运算符:

my_function(String ~ ["^[A-Za-z]+[A-Za-z0-9]*$"]) ->
    ....

我知道正则表达式模块(re),但AFAIK你不能在模式匹配或守卫时调用函数。

另外,我希望匹配字符串可以以不区分大小写的方式完成。这很方便,例如,在解析 HTTP 标头时,我想做这样的事情,其中​​“Str ~ {Pattern, Options}”表示“使用选项选项将 Str 与模式模式匹配”:

handle_accept_language_header(Header ~ {"Accept-Language", [case_insensitive]}) ->
    ...

两个问题:

  1. 您通常如何仅使用标准 Erlang 来处理这个问题?是否有一些机制/编码风格在简洁性和易读性方面接近于此?

  2. Erlang 是否有任何工作(EEP?)来解决这个问题?

4

6 回答 6

7

除了提前运行正则表达式然后对结果进行模式匹配之外,您实际上别无选择。这是一个非常简单的示例,它接近我认为您所追求的,但它确实存在您需要重复正则表达式两次的缺陷。您可以通过使用宏在一个地方定义每个正则表达式来减轻痛苦。

-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]).
于 2009-11-02T15:35:47.150 回答
6

一种可能是结合使用Erlang Web 样式的注释(宏)和re Erlang 模块。一个例子可能是说明这一点的最佳方式。

这就是您的最终代码的样子:

[...]
?MATCH({Regexp, Options}).
foo(_Args) ->
  ok.
[...]

MATCH宏将在您的foo函数之前执行。如果正则表达式模式不匹配,执行流程将失败。

您的 match 函数将声明如下:

?BEFORE.
match({Regexp, Options}, TgtMod, TgtFun, TgtFunArgs) ->
String = proplists:get_value(string, TgtArgs),
case re:run(String, Regexp, Options) of
  nomatch ->
    {error, {TgtMod, match_error, []}};
  {match, _Captured} ->
    {proceed, TgtFunArgs}
end.

请注意:

  • BEFORE表示宏将在您的目标函数之前执行(AFTER 宏也可用)。
  • match_error 是您的错误处理程序,在您的模块中指定,并且包含您想要在匹配失败时执行的代码(可能什么都没有,只是阻止执行流程)
  • 这种方法的优点是使正则表达式语法和选项与re模块保持一致(避免混淆)。

有关 Erlang Web 注释的更多信息,请点击此处:

http://wiki.erlang-web.org/Annotations

和这里:

http://wiki.erlang-web.org/HowTo/CreateAnnotation

该软件是开源的,因此您可能希望重用他们的注释引擎。

于 2009-11-02T16:16:54.710 回答
3

您可以使用 re 模块:

re:run(String, "^[A-Za-z]+[A-Za-z0-9]*$").
re:run(String, "^[A-Za-z]+[A-Za-z0-9]*$", [caseless]).

编辑:

match(String, Regexps) -> 
  case lists:dropwhile(
               fun({Regexp, Opts}) -> re:run(String, Regexp, Opts) =:= nomatch;
                  (Regexp) -> re:run(String, Regexp) =:= nomatch end,
               Regexps) of
    [R|_] -> R;
    _     -> nomatch
  end.

example(String) ->
  Regexps = ["$RE1^", {"$RE2^", [caseless]}, "$RE3"]
  case match(String, Regexps) of
    nomatch -> handle_error();
    Regexp -> handle_regexp(String, Regexp)
    ...
于 2009-11-02T11:29:47.473 回答
3
  1. 对于字符串,您可以使用“re”模块:之后,您可以遍历结果集。恐怕没有另一种方法可以做到这一点AFAIK:这就是为什么有正则表达式。

  2. 对于 HTTP 标头,因为可能有很多,我会考虑迭代结果集是一个更好的选择,而不是编写一个很长的表达式(可能)。

  3. EEP工作:我不知道。

于 2009-11-02T11:35:00.880 回答
2
  1. Erlang 不处理模式中的正则表达式。
  2. 不。
于 2009-11-05T00:22:56.180 回答
1

抱歉,您不能对正则表达式进行模式匹配。所以你必须做

my_function(String) -> Matches = re:run(String, "^[A-Za-z]+[A-Za-z0-9]*$"),
                       ...
于 2009-11-02T14:01:20.450 回答