Erlang 正则过滤空格 包括全角空格
之前在验证玩家起名字的时候,首先先去掉字符串首尾空格,写了一个简单的trim_string
1 trim_string(Str) when is_binary(Str) -> 2 trim_string(Str, binary); 3 trim_string(Str) when is_list(Str) -> 4 trim_string(Str, list). 5 6 trim_string(Str, Ret) -> 7 re:replace(Str, "^[\s]+|[\s]+$", "", [global,{return, Ret}]).
发现还有人成功起了空白的名字,经查明是一个全角空格,Unicode编码是\u3000,于是优化为
trim_string(Str) when is_binary(Str) -> trim_string(Str, binary); trim_string(Str) when is_list(Str) -> trim_string(Str, list). %% 不加unicode运行会报参数错误 trim_string(Str, Ret) -> re:replace(Str, "^[\s\x{3000}]+|[\s\x{3000}]+$", "", [global,{return, Ret},unicode]).