Regex 使用Erlang正则表达式删除空白
我想删除所有空格,即制表符/空格/换行符Regex 使用Erlang正则表达式删除空白,regex,erlang,Regex,Erlang,我想删除所有空格,即制表符/空格/换行符 T = {xmlelement,"presence", [{"xml:lang","en"}], [{xmlcdata,<<"\n">>}, {xmlelement,"priority"
T = {xmlelement,"presence",
[{"xml:lang","en"}],
[{xmlcdata,<<"\n">>},
{xmlelement,"priority",[],
[{xmlcdata,<<"5">>}]},
{xmlcdata,<<"\n">>},
{xmlelement,"c",
[{"xmlns",
"http://jabber.org/protocol/caps"},
{"node","http://psi-im.org/caps"},
{"ver","0.12.1"},
{"ext","cs ep-notify html"}],
[]},
{xmlcdata,<<"\n">>}]}.
如果要删除字符串中的所有内容,需要将全局选项传递给re:replace。您也只使用该正则表达式替换换行符。该调用可能如下所示:
trim_whitespace(Input) -> re:replace(Input, "\\s+", "", [global]).
如果要删除字符串中的所有内容,需要将全局选项传递给re:replace。您也只使用该正则表达式替换换行符。该调用可能如下所示:
trim_whitespace(Input) -> re:replace(Input, "\\s+", "", [global]).
您问题中的所有空白都在cdata区域中-为什么不直接从元组中过滤这些空白呢
remove_cdata(List) when is_list(List) ->
remove_list_cdata(List);
remove_cdata({xmlelement, Name, Attrs, Els}) ->
{xmlelement, Name, remove_cdata(Attrs), remove_cdata(Els)}.
remove_list_cdata([]) ->
[];
remove_list_cdata([{xmlcdata,_}|Rest]) ->
remove_list_cdata(Rest);
remove_list_cdata([E = {xmlelement,_,_,_}|Rest]) ->
[remove_cdata(E) | remove_list_cdata(Rest)];
remove_list_cdata([Item | Rest]) ->
[Item | remove_list_cdata(Rest)].
remove_cdata(T) =:=
{xmlelement,"presence",
[{"xml:lang","en"}],
[{xmlelement,"priority",[],[]},
{xmlelement,"c",
[{"xmlns","http://jabber.org/protocol/caps"},
{"node","http://psi-im.org/caps"},
{"ver","0.12.1"},
{"ext","cs ep-notify html"}],
[]}]}
您问题中的所有空白都在cdata区域中-为什么不直接从元组中过滤这些空白呢
remove_cdata(List) when is_list(List) ->
remove_list_cdata(List);
remove_cdata({xmlelement, Name, Attrs, Els}) ->
{xmlelement, Name, remove_cdata(Attrs), remove_cdata(Els)}.
remove_list_cdata([]) ->
[];
remove_list_cdata([{xmlcdata,_}|Rest]) ->
remove_list_cdata(Rest);
remove_list_cdata([E = {xmlelement,_,_,_}|Rest]) ->
[remove_cdata(E) | remove_list_cdata(Rest)];
remove_list_cdata([Item | Rest]) ->
[Item | remove_list_cdata(Rest)].
remove_cdata(T) =:=
{xmlelement,"presence",
[{"xml:lang","en"}],
[{xmlelement,"priority",[],[]},
{xmlelement,"c",
[{"xmlns","http://jabber.org/protocol/caps"},
{"node","http://psi-im.org/caps"},
{"ver","0.12.1"},
{"ext","cs ep-notify html"}],
[]}]}
我也面临同样的问题…来这里分享我更高效的工作:
trim(Subject) ->
{match, [[Trimmed]|_]} = re:run(Subject, "^\\s*([^\\s]*(?:.*[^\\s]+)?)\\s*$",
[{capture, all_but_first, binary}, global, dollar_endonly, unicode, dotall]),
Trimmed.
这个想法非常相似。正则表达式更好。我也面临同样的问题……来这里分享我更高效的工作:
trim(Subject) ->
{match, [[Trimmed]|_]} = re:run(Subject, "^\\s*([^\\s]*(?:.*[^\\s]+)?)\\s*$",
[{capture, all_but_first, binary}, global, dollar_endonly, unicode, dotall]),
Trimmed.
这个想法非常相似。正则表达式更好。re:replace很棘手,需要记住:
Eshell V5.9.3.1 (abort with ^G)
1> re:replace("0 1 2 3 4 5 6 7 8 9", " ", "", [global, {return, list}]).
"0123456789"
2> re:replace("0 1 2 3 4 5 6 7 8 9", " ", "", [{return, list}]).
"01 2 3 4 5 6 7 8 9"
3> re:replace("0 1 2 3 4 5 6 7 8 9", " ", "").
[<<"0">>,[]|<<"1 2 3 4 5 6 7 8 9">>]
回复:更换很棘手,需要记住:
Eshell V5.9.3.1 (abort with ^G)
1> re:replace("0 1 2 3 4 5 6 7 8 9", " ", "", [global, {return, list}]).
"0123456789"
2> re:replace("0 1 2 3 4 5 6 7 8 9", " ", "", [{return, list}]).
"01 2 3 4 5 6 7 8 9"
3> re:replace("0 1 2 3 4 5 6 7 8 9", " ", "").
[<<"0">>,[]|<<"1 2 3 4 5 6 7 8 9">>]
我得到以下错误:套接字:trim_whitespaceP.*异常错误:函数re:replace/4 socket:trim_whitespaceP中的参数错误。**异常错误:函数re:replace/4中的参数错误,称为re:replace{xmlement,presence,…。我得到以下错误:套接字:trim_whitespaceP.**异常错误:函数re:replace/4中的参数错误套接字:trim_whitespaceP.**异常错误:函数re:replace/4中的参数错误,称为re:replace{xmlement,presence,…。我希望删除所有字符。\r\n我希望它显示在一行中,以便发送给我的perl程序。为此,我需要删除整个元组中的所有字符。\r\n现在每个逗号后面都有一个新行字符。如何将所有字符压缩到一行中?是否要将该erlang元组序列化为不带换行符/回车符的字符串?我希望删除所有\r\n字符。我希望它显示在一行中,以便将其发送到我的perl程序。为此,我需要从整个元组中删除\r\n所有字符。现在,在每个逗号之后都有一个新行字符。我如何压缩将所有内容都放在一行中?是否要将该erlang元组序列化为一个字符串,而不返回换行符/回车符?