【问题标题】:How to convert accented strings to regular strings in Erlang?如何在 Erlang 中将重音字符串转换为常规字符串?
【发布时间】:2021-10-13 18:29:50
【问题描述】:

我想将一些带有重音字符的城市名称转换为普通字符串。例如:

<<"Sosúa">>  to  <<"Sosua">>

<<"Luperón">> to <<"Luperon">>

关于如何做到这一点的任何线索?

【问题讨论】:

标签: string unicode erlang diacritics


【解决方案1】:
  1. 应用Unicode Canonical Decomposition (NFD) 来重写两个代码点o (U+6F) 中的ó 等字符,后跟unicode:characters_to_nfc_binary/1 的分隔组合重音符号(U+301)
  2. 使用正则表达式 \p{Mn},替换 (re:replace/4) 所有组合变音符号(非空格标记)的所有那些,如上面的 U+301
  3. 可选:应用 Unicode 规范组合 (NFC) 将剩余和可能的代码点重新组合在一起
String = "Luperón",
{ok, Re} = re:compile("\\p{Mn}", [unicode]),
Output = unicode:characters_to_nfc_binary(
  re:replace(
    unicode:characters_to_nfd_binary(String),
    Re,
    "",
    [global]
  )
),
Output.

等效于 Elixir,供参考和信息(因为它也是基于 Erlang 的 unicode 模块):

string = "Luperón"
output = 
  Regex.replace(~R<\p{Mn}>u, string |> :unicode.characters_to_nfd_binary(), "")
  |> :unicode.characters_to_nfc_binary()

【讨论】:

  • 当问题是特定于 Erlang 时,我不明白为什么要在您的答案中添加 Elixir 代码示例?许多语言都在使用 Erlang 及其底层模块,所以这对我来说不是一个理由。其次:unicode不是基于Erlang,它实际上是一个Erlang Module。
  • 哎呀!!!从答案编辑历史中得到它,看起来你是从 Elixir 代码开始的......很好!我感到很愚蠢:\谢谢你的回答
猜你喜欢
  • 2016-12-10
  • 1970-01-01
  • 2011-05-23
  • 2011-05-12
  • 1970-01-01
  • 2010-12-30
  • 1970-01-01
  • 2012-04-18
  • 2018-10-29
相关资源
最近更新 更多