删除括号内的空格的正则表达式是什么? [英] What is the regular expression to remove whitespace inside brackets?

查看:308
本文介绍了删除括号内的空格的正则表达式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java编写一个程序来接受查询。如果我有一个查询,如

I am writing a program in Java to accept queries. If I have a query like

insert    
into  
abc      values   (    e   
, b    );

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

...what regular expression can I use to convert that into:

insert into abc values(e,b);

...或者:

insert:into:abc:values(e,b);

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

Actually I want to know how I can I write a regular expression to remove whitespace within brackets only.

推荐答案

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

Assuming correctly balanced parentheses, and no nested parentheses, the following will remove all whitespace within parentheses (and only there):

String resultString = subjectString.replaceAll("\\s+(?=[^()]*\\))", "");

它转换

insert    
into  
abc      values   (    e   
, b    );

进入

insert    
into  
abc      values   (e,b);

说明:

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

这篇关于删除括号内的空格的正则表达式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆