replace(Subject, RE, Replacement, Options) ->
 iodata() | unicode:charlist()
Subject = iodata() | unicode:charlist()
RE = mp() | iodata() | unicode:charlist()
Replacement = iodata() | unicode:charlist() | replace_fun()
Options = [Option]
Option = 
    anchored | global | notbol | noteol | notempty |
    notempty_atstart |
    {offset, integer() >= 0} |
    {newline, NLSpec} |
    bsr_anycrlf |
    {match_limit, integer() >= 0} |
    {match_limit_recursion, integer() >= 0} |
    bsr_unicode |
    {return, ReturnType} |
    CompileOpt
ReturnType = iodata | list | binary
CompileOpt = compile_option()
NLSpec = cr | crlf | lf | anycrlf | any
將 Subject 字符串的匹配部分替換為 Replacement 。
允許的選項與run/3,除了該選項
          capture不允許。相反,{return,
          ReturnType}存在。默認返回類型是iodata,以盡量減少複製的方式構建。這iodata結果可以直接用於許多 I/O 操作。如果是平房list()如果需要,請指定{return, list}。如果需要二進製文件,請指定{return, binary}.
與函數 run/3 一樣,使用選項 unicode 編譯的 mp() 要求 Subject 為 Unicode charlist() 。如果編譯是隱式完成的,並且為此函數指定了 unicode 編譯選項,則正則表達式和 Subject 都將指定為有效的 Unicode charlist()。
如果替換以字符串形式給出,則它可以包含特殊字符 & ,它將在結果中插入整個匹配表達式,以及特殊序列 \ N (其中 N 是大於 0 的整數)、\g N 或 \g{ N } 生成子表達式編號 N,並將其插入到結果中。如果正則表達式沒有生成具有該編號的子表達式,則不會插入任何內容。
要在結果中插入 & 或 \,請在其前麵添加 \。請注意,Erlang 已經在文字字符串中賦予了特殊含義,因此單個 \ 必須寫為 "\\" ,因此雙 \ 必須寫為 "\\\\" 。
例子:
re:replace("abcd","c","[&]",[{return,list}]).
給
"ab[c]d"
盡管
re:replace("abcd","c","[\\&]",[{return,list}]).
給
"ab[&]d"
如果替換作為 fun 給出,則將使用整個匹配表達式作為第一個參數和子表達式匹配列表(按照它們在正則表達式中出現的順序進行匹配)來調用。返回的值將被插入到結果中。
例子:
re:replace("abcd", ".(.)", fun(Whole, [<<C>>]) -> <<$#, Whole/binary, $-, (C - $a + $A), $#>> end, [{return, list}]).
給
"#ab-B#cd"
如果非匹配的可選子表達式是正則表達式中的最後一個子表達式,則它們不會包含在子表達式匹配列表中。
例子:
正則表達式"(a)(b)?(c)?"("a",可選地後跟"b",可選地後跟"c")將創建以下子表達式列表:
[<<"a">>, <<"b">>, <<"c">>]應用於字符串"abc"[<<"a">>, <<>>, <<"c">>]應用於字符串"acx"[<<"a">>, <<"b">>]應用於字符串"abx"[<<"a">>]應用於字符串"axx"
與run/3,編譯錯誤會引發badarg異常。compile/2可用於獲取有關錯誤的更多信息。
相關用法
- erlang replace(Subject, Pattern, Replacement, Options)用法及代碼示例
 - erlang replace(String, SearchPattern, Replacement)用法及代碼示例
 - erlang repair_continuation(Continuation, MatchSpec)用法及代碼示例
 - erlang reverse用法及代碼示例
 - erlang rename用法及代碼示例
 - erlang remove用法及代碼示例
 - erlang registered用法及代碼示例
 - erlang register用法及代碼示例
 - erlang register(RegName, PidOrPort)用法及代碼示例
 - erlang registered()用法及代碼示例
 - erlang read_file_info(Filename)用法及代碼示例
 - erlang read_file_info(File)用法及代碼示例
 - erlang remove(Key, Map1)用法及代碼示例
 - erlang referenced_byte_size(Binary)用法及代碼示例
 - erlang receive_response(RequestId, Timeout)用法及代碼示例
 - erlang reverse(String :: unicode:chardata())用法及代碼示例
 - erlang reverse(List1, Tail)用法及代碼示例
 - erlang relation_to_family(BinRel)用法及代碼示例
 - erlang relative_product(ListOfBinRels)用法及代碼示例
 - erlang relative_product1(BinRel1, BinRel2)用法及代碼示例
 - erlang restriction(BinRel1, Set)用法及代碼示例
 - erlang restriction(SetFun, Set1, Set2)用法及代碼示例
 - erlang recv(Socket)用法及代碼示例
 - erlang recompose(URIMap)用法及代碼示例
 - erlang resolve(RefURI, BaseURI)用法及代碼示例
 
注:本文由純淨天空篩選整理自erlang.org大神的英文原創作品 replace(Subject, RE, Replacement, Options) -> iodata() | unicode:charlist()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
