【发布时间】:2017-09-20 10:29:12
【问题描述】:
我不擅长正则表达式并试图实现以下场景
String s="Example$for$string%is%notworking";
现在我需要将符号符号之间的字符串替换为其他字符串
Examplewithstringarenotworking
我正在使用
s=s.replaceall("\\$(.*?)\\%", "bird");
但上面的表达式没有发生任何变化
【问题讨论】:
我不擅长正则表达式并试图实现以下场景
String s="Example$for$string%is%notworking";
现在我需要将符号符号之间的字符串替换为其他字符串
Examplewithstringarenotworking
我正在使用
s=s.replaceall("\\$(.*?)\\%", "bird");
但上面的表达式没有发生任何变化
【问题讨论】:
试试下面的代码:
String s="Example$for$string%is%notworking";
s.replaceAll("[$&+,:;=?@#|'<>.^*()%!-]", "otherstring")
但在上述方法中,我们没有考虑许多特殊字符,例如一些 DOS 表情符号,如 little angle 和 white smily face
所以可能需要尝试一些相反的事情。哪些是您要保留的字符。如下所示,我将 A-Z、a-z 和 0-9 取如下:
s.replaceAll("[^A-Za-z0-9]", "otherstring")
【讨论】: