Muh*_*tra 10 java regex string brackets
我想通过使用java删除字符串中的所有类型的括号字符(例如:[],(),{}).
我尝试使用此代码:
String test = "watching tv (at home)";
test = test.replaceAll("(","");
test = test.replaceAll(")","");
Run Code Online (Sandbox Code Playgroud)
但它没有用,请帮帮我.
Rei*_*eus 23
第一个参数replaceAll采用正则表达式.
所有括号在正则表达式中都有意义:括号在正则表达式中用于引用捕获组,方括号用于字符类,括号用于匹配的字符出现.因此它们都需要被转义...但是这里的字符可以简单地包含在一个字符类中,只需要方括号转义
test = test.replaceAll("[\\[\\](){}]","");
Run Code Online (Sandbox Code Playgroud)
Sta*_*wed 19
删除包含所有括号,大括号和sq括号的所有标点符号...根据问题是:
String test = "watching tv (at home)";
test = test.replaceAll("\\p{P}","");
Run Code Online (Sandbox Code Playgroud)
传递给该方法的第一个参数replaceAll()应该是正则表达式。如果你想匹配那些文字括号字符,你需要转义\\(,\\)它们。
您可以使用以下命令删除括号字符。Unicode 属性\p{Ps}将匹配任何类型的左括号,Unicode 属性\p{Pe}将匹配任何类型的右括号。
String test = "watching tv (at home) or [at school] or {at work}()[]{}";
test = test.replaceAll("[\\p{Ps}\\p{Pe}]", "");
System.out.println(test); //=> "watching tv at home or at school or at work"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
54140 次 |
| 最近记录: |