Erlang 如何引用正则表达式字符串中的所有元字符

Erlang 如何引用正则表达式字符串中的所有元字符,erlang,Erlang,我需要用其他字符串替换子字符串,字符串:,但re: 然而,为了使用re:replace,我需要引用所有特定于正则表达式的元字符,如[.等 在ocaml中,它被称为Str.quote val quote : string -> string Str.quote s returns a regexp string that matches exactly s and nothing else. 从 这个函数在Erlang中被称为什么?在Elixir中找到它,名为regex:escape

我需要用其他字符串替换子字符串,字符串:,但re:

然而,为了使用re:replace,我需要引用所有特定于正则表达式的元字符,如[.等

在ocaml中,它被称为Str.quote

 val quote : string -> string

 Str.quote s returns a regexp string that matches exactly s and nothing else.


这个函数在Erlang中被称为什么?

在Elixir中找到它,名为regex:escape。转换为Erlang后,它看起来是这样的(需要查看unicode并返回二进制标志)


<>而不是引用ReGEXP特殊字符,您应该考虑将字符串转换为二进制,并使用.< /P>
escape(String) ->
    re:replace(String, "[.^$*+?()[{\\\|\s#]", "\\\\&",[global]).
{:ok, pattern} = :re.compile(~S"[.^$*+?()[{\\\|\s#]", [:unicode])
@escape_pattern pattern

def escape(string) when is_binary(string) do
    :re.replace(string, @escape_pattern, "\\\\&", [:global, {:return, :binary}])
end