java中,正则表达式,如何过滤除数字和字母之外的其它字符??

2024-12-02 09:28:46
推荐回答(3个)
回答1:

String s = "sf9897&^%fdferf";

s = s.replaceAll("[^0-9a-zA-Z]","");
System.out.print(s);

回答2:

String str = "sf9%897&^%fdferf";
str = str .replaceAll("[^0-9a-zA-Z]+","");
System.out.print(str);

回答3:

\w A word character: [a-zA-Z_0-9]
\W A non-word character: [^\w]