我想通过正则表达式从字符串末尾删除 )
字符。
例如,如果一个字符串是 UK(Great Britain),那么我想替换最后一个 )
符号。
注意:
1).正则表达式应该只删除最后一个 )
符号,与字符串中存在多少个 )
符号无关。
最佳答案
请不要使用正则表达式来完成这个简单的任务。
// If the last ) might not be the last character of the String
String s = "Your String with) multiple).";
StringBuilder sb = new StringBuilder(s);
sb.deleteCharAt(s.lastIndexOf(')'));
s = sb.toString(); // s = "Your String with) multiple."
// If the last ) will always be the last character of the String
s = "Your String with))";
if (s.endsWith(")"))
s = s.substring(0, s.length() - 1);
// s = "Your String with)"
关于Java - 正则表达式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2646773/