删除特殊字符前后的空格

Ros*_*nck 0 java string

我有一个字符串,

String sample = "He won the International Rating Chess Tournament (IRCT ) which concluded on Dec. 22 at the Blue Sky Hotel , Canada";
Run Code Online (Sandbox Code Playgroud)

我想删除字符')'和','之前留下的空格(如果有的话).所以最终输出应该是,

He won the International Rating Chess Tournament (IRCT) which concluded on Dec. 22 at the Blue Sky Hotel, Canada
Run Code Online (Sandbox Code Playgroud)

谁能告诉我怎么做?

Luk*_*der 6

sample.replaceAll("\\s+(?=[),])", "");
Run Code Online (Sandbox Code Playgroud)

即删除\s+紧跟(?=...)任何以下字符的所有空格[),].双反斜杠用于逃逸.有关正则表达式的更多信息,请参见此处:

http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html