[英]replace any non-ascii character in a string in java
一個人怎么會轉換-lrb-300-rrb-┬á922-6590
到-lrb-300-rrb- 922-6590
在java中?
嘗試了以下內容:
t.lemma = lemma.replaceAll("\\p{C}", " ");
t.lemma = lemma.replaceAll("[\u0000-\u001f]", " ");
我可能錯過了概念性的東西 將欣賞任何解決方案的指針。
謝謝
嘗試下一個:
str = str.replaceAll("[^\\\\p{ASCII}]", " ");
順便說一句, \\p{ASCII}
都是ASCII: [\\x00-\\x7F]
。
另外,您需要使用Pattern
的常量,以避免每次重新編譯表達式。
private static final Pattern REGEX_PATTERN =
Pattern.compile("[^\\p{ASCII}]");
public static void main(String[] args) {
String input = "-lrb-300-rrb- 922-6590";
System.out.println(
REGEX_PATTERN.matcher(input).replaceAll(" ")
); // prints "-lrb-300-rrb- 922-6590"
}
也可以看看:
假設你只想保留a-zA-Z0-9
和標點字符,你可以這樣做:
t.lemma = lemma.replaceAll("[^\\p{Punct}\\w]", " "));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.