替换java中字符串中的任何非ascii字符

leb*_*lev 4 java regex unicode

如何在java中转换-lrb-300-rrb-?á922-6590-lrb-300-rrb- 922-6590

尝试了以下内容:

t.lemma = lemma.replaceAll("\\p{C}", " ");
t.lemma = lemma.replaceAll("[\u0000-\u001f]", " ");
Run Code Online (Sandbox Code Playgroud)

我可能错过了概念性的东西 将欣赏任何解决方案的指针.

谢谢

Pau*_*gas 10

尝试下一个:

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"
}
Run Code Online (Sandbox Code Playgroud)

也可以看看: