Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我在替换 Java 中的一些字符时遇到了很大的问题。我想删除所有不是字母、数字或特殊国家字符(例如“ę, ą”)的字符。当我使用该功能时,replaceAll("\W", " ")特殊字符也被删除。
replaceAll("\W", " ")
示例字符串:“Jest źle, ale będzie lepiej。”
如何替换:“Jest le ale b dzie lepiej”
应该如何:“Jest źle ale będzie lepiej”
对不起我的英语不太好:)
你的英语比 Java 的波兰语好。Java 的正则表达式不会说波兰语,因此它只考虑 a..z “国家字符”(加上数字和下划线——GREP 显然是由程序员设计的)。这很公平,实际上:一种语言的“正常”字符对于另一种语言来说是“奇怪的”。
您可以在自定义否定字符类中总结一些额外的非 ASCII 字符:
replace ("[^\wźę ]", " ");
(您还应该添加其他重音字符,并可能删除非波兰语字符,例如 Q 和 X)。