我想用字符串中任何括号内的下划线替换短划线 示例字符串:
[a]-[a-gamma]+(a-alpha)*{a}-{b-gamma}+[a]
的replaceAll =?
输出
[a]-[a_gamma]+(a_alpha)*{a}-{b_gamma}+[a]
答案 0 :(得分:2)
尝试使用lookbehind mechanism in regexp
执行此操作String input = "[a]-[a-gamma]+(a-alpha)*{a}-{b-gamma}+[a]";
String result = input.replaceAll("-(?![\\[\\{\\(])","_");