删除括号内的空格的正则表达式是什么?

Ars*_*war 4 java regex

我正在用Java编写一个程序来接受查询.如果我有一个像这样的查询

insert    
into  
abc      values   (    e   
, b    );
Run Code Online (Sandbox Code Playgroud)

...我可以使用什么正则表达式将其转换为:

insert into abc values(e,b);
Run Code Online (Sandbox Code Playgroud)

...要么:

insert:into:abc:values(e,b);
Run Code Online (Sandbox Code Playgroud)

其实我想知道如何编写正则表达式来删除括号内的空格.

Tim*_*ker 12

假设正确平衡括号,并且没有嵌套括号,以下将删除括号内的所有空格(并且仅在那里):

String resultString = subjectString.replaceAll("\\s+(?=[^()]*\\))", "");
Run Code Online (Sandbox Code Playgroud)

它转变

insert    
into  
abc      values   (    e   
, b    );
Run Code Online (Sandbox Code Playgroud)

insert    
into  
abc      values   (e,b);
Run Code Online (Sandbox Code Playgroud)

说明:

\s+      # Match whitespace
(?=      # only if followed by...
 [^()]*  # any number of characters except parentheses
 \)      # and a closing parenthesis
)        # End of lookahead assertion
Run Code Online (Sandbox Code Playgroud)