简体   繁体   English

用于删除特殊字符的正则表达式

[英]regex for removing special characters

I need a kotlin function that will remove specific special characters from a string.我需要一个 kotlin function 来从字符串中删除特定的特殊字符。

The list of the special chars: []'()*"'特殊字符列表: []'()*"'
Data also contains €˜€™ Ÿ'Ÿ数据还包含€˜€™ Ÿ'Ÿ

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


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

I tested我测试了

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"

But got:但是得到了:

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

What is wrong?怎么了?

It is necessary to escape special characters [ ] ( ) * |需要转义特殊字符 [ ] ( ) * |

And surround all the characters you are looking for with square brackets [ ]并用方括号 [ ] 包围您要查找的所有字符

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, ""))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM