Java – what is a regular expression that removes spaces in parentheses?
•
Java
I'm writing a program in Java to accept queries If I had a query like this
insert into abc values ( e,b );
... what regular expressions can I use to convert it to:
insert into abc values(e,b);
... or:
insert:into:abc:values(e,b);
In fact, I want to know how to write regular expressions to remove spaces in parentheses
Solution
Assuming the parentheses are balanced correctly and there are no nested parentheses, the following will remove all spaces within the parentheses (and only there):
String resultString = subjectString.replaceAll("\\s+(?=[^()]*\\))","");
Its transformation
insert into abc values ( e,b );
become
insert into abc values (e,b);
explain:
\s+ # Match whitespace (?= # only if followed by... [^()]* # any number of characters except parentheses \) # and a closing parenthesis ) # End of lookahead assertion
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码