I am writing a program in Java to accept queries. If I have a query like
insert
into
abc values ( e
, b );
...what re
As far as I see you don't need RegEx for this. That should work:
yourString.replaceAll("\n", " ").replaceAll("( ","(").replaceAll(" )",")").replaceAll(", ",",").replaceAll(" ,",",");
Assuming correctly balanced parentheses, and no nested parentheses, the following will remove all whitespace within parentheses (and only there):
String resultString = subjectString.replaceAll("\\s+(?=[^()]*\\))", "");
It transforms
insert
into
abc values ( e
, b );
into
insert
into
abc values (e,b);
Explanation:
\s+ # Match whitespace
(?= # only if followed by...
[^()]* # any number of characters except parentheses
\) # and a closing parenthesis
) # End of lookahead assertion