kotlin 用于删除特殊字符的正则表达式[已关闭]

rggaifut  于 2023-01-26  发布在  Kotlin
关注(0)|答案(1)|浏览(226)

16小时前关门了。
Improve this question
我需要一个kotlin函数,将从字符串中删除特定的特殊字符。
特殊字符的列表:[]‘()*"'
数据还包含\u0080\u0098\u0080\u0099 \u009F\u0091\u009F

private val specialCharRemoval = "[‘()*’\\\"|\u0099|\u009C|']".toRegex()

fun deaccent(keyword: String): String {
    keyword.replace(specialCharRemoval, "")
}

我测试了

String tokens = "\"book*‘\"  ¼ ¶childΩ 7 â\u0080\u0098play* for| the  â " +
                "future\u0080\u0099 sizeð\u009F\u0091\u009F11.5 (men's)"

String expected = "book  ¼ ¶childΩ 7 play for the â future sizeð 11.5 mens"

但得到了:

String actual = ""book*‘"  ¼ ¶childΩ 7 ‘play* for| the  â future€™ size👟11.5 (men’s)"

怎么了?

6ie5vjzr

6ie5vjzr1#

必须转义特殊字符[ ]()*|
并将您要查找的所有字符用方括号[ ]括起来

val str = "\"book*‘\"  ¼ ¶childΩ 7 â\u0080\u0098play* for| the  â future\u0080\u0099 sizeð\u009F\u0091\u009F11.5 (men's)"
val regex = "[\\[\\]‘\\(\\)\\*\"'\\|\u0080\u0098\u0080\u0099\u009F\u0091\u009F]".toRegex()

println(str.replace(regex, ""))

相关问题