使用 Erlang Regex 删除空格
我想删除所有空白,即制表符/空格/换行符。
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">>}]}.
我尝试了以下方法,但它不起作用:
trim_whitespace(Input) ->
re:replace(Input, "(\r\n)*", "").
I want to remove all the whitespace i..e tabs/spaces/newline chars.
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">>}]}.
I tried the following, but it does not work:
trim_whitespace(Input) ->
re:replace(Input, "(\r\n)*", "").
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果要删除字符串中的所有内容,则需要将全局选项传递给 re:replace()。 您也只是使用该正则表达式替换换行符。 该调用可能应该如下所示:
If you want to remove everything in a string, you need to pass the global option to re:replace(). You're also only replacing newlines by using that regex. The call should probably look like this:
我遇到了同样的问题......来这里分享我更高效的工作:
想法非常相似。 正则表达式更好。
I faced the same issue… came here to share my more efficient work:
The idea is very much the same. The regex is just better.
您问题中的所有空白都在 cdata 部分中 - 为什么不直接从元组中过滤掉这些空白呢?
All the whitespace in your question is in cdata sections - why not just filter those out of the tuple?
re:replace 很棘手,需要记住:
re:replace is tricky, something to keep in mind: