Java 将所有出现的{.?!“gt;”lt;&“x27;”、/\\\\\$%^&*~`-\=&“x2B;”替换为一个单词“quot;“我的话”;
例如: 输入Java 将所有出现的{.?!“gt;”lt;&“x27;”、/\\\\\$%^&*~`-\=&“x2B;”替换为一个单词“quot;“我的话”;,java,replace,pattern-matching,replaceall,Java,Replace,Pattern Matching,Replaceall,例如: 输入字符串为 Input = "Hello there, How are you? Fine!! @xyz" 输出将是 Output = "Hello there myWord How are you myWord Fine myWord myWord myWord xyz" 我尝试了Pattern类和Matcher类,但它只替换了一种类型的模式和str.replace(“.”,myWord”)您可以使用[^\\w\\s] \\s*[^\\w\\s]\\s*:\\s*表示一个或多
字符串
为
Input = "Hello there, How are you? Fine!! @xyz"
输出将是
Output = "Hello there myWord How are you myWord Fine myWord myWord myWord xyz"
我尝试了
Pattern
类和Matcher
类,但它只替换了一种类型的模式和str.replace(“.”,myWord”)代码>您可以使用[^\\w\\s]
\\s*[^\\w\\s]\\s*
:\\s*
表示一个或多个空格
[^\\w\\s]
:^
不要捕获\\w
和\\s
\\w
平均值a-zA-Z0-9
\\s
平均空间
输出:
Hello there myWord How are you myWord Fine myWord myWord myWord xyz
为了避免任何其他不应替换的特殊字符,只需将它们添加到此[]
中即可,例如\\s*[^\\w\\s:;\\[\]]\\s*
,如@brso05所述
演示
const regex=/\s*[^\w\s\]\[;]\s*/g;
const str=`你好,你好吗?很好@xyz[]`;
const subst=`myWord`;
const result=str.replace(regex,subst);
控制台日志(结果)代码>您可以使用[^\\w\\s]
\\s*[^\\w\\s]\\s*
:\\s*
表示一个或多个空格
[^\\w\\s]
:^
不要捕获\\w
和\\s
\\w
平均值a-zA-Z0-9
\\s
平均空间
输出:
Hello there myWord How are you myWord Fine myWord myWord myWord xyz
为了避免任何其他不应替换的特殊字符,只需将它们添加到此[]
中即可,例如\\s*[^\\w\\s:;\\[\]]\\s*
,如@brso05所述
演示
const regex=/\s*[^\w\s\]\[;]\s*/g;
const str=`你好,你好吗?很好@xyz[]`;
const subst=`myWord`;
const result=str.replace(regex,subst);
控制台日志(结果)代码>您可以执行以下操作:
String str = "Hello there, How are you? Fine!! @xyz";
str = str.replaceAll("[\\{\\.\\?!\"><',/\\\\\\|@#$%^&\\*~`\\-_=+\\}]", "myWord");
System.out.println(str);
String str=“你好,你好吗?很好!!@xyz”;
str=str.replaceAll(“[\{\\.\?!\”>您可以执行以下操作:
String str = "Hello there, How are you? Fine!! @xyz";
str = str.replaceAll("[\\{\\.\\?!\"><',/\\\\\\|@#$%^&\\*~`\\-_=+\\}]", "myWord");
System.out.println(str);
String str=“你好,你好吗?很好!!@xyz”;
str=str.replaceAll(“[\{\.\\?!\”>你的意思是所有出现在花括号中的符号,或者花括号也是你想要替换的符号?我也想移除花括号…谢谢你的回答你的意思是所有出现在花括号中的符号,或者花括号也是你想要替换的符号?我想移除cu谢谢你的回答为什么投反对票?为什么投反对票?