user2589299
user2589299

Reputation: 129

removing single line SQL comments using java

How do you remove single line SQL comments froma single string using Java? I tried something like the following, but this doesnot seem to be fool-proof. Need a regex that will account for '--' characters when they appear as literals in select statements as in select '--hi' from dual.

   protected String removeSingleLineComments(String sql)
{

  Pattern pattern = Pattern.compile("--[^\r\n]*");
  Matcher matcher = pattern.matcher(sql);


  while(matcher.find()) {


      if((matcher.start()==0) || (matcher.start()>0 && sql.charAt(matcher.start()-1) != '\''))
  {
      sql =sql.replace(sql.substring(matcher.start(), matcher.end()), "").trim();


  }
  }
  return sql;

} 

Upvotes: 1

Views: 1854

Answers (3)

Ian B
Ian B

Reputation: 11

Just split the string by carriage return then split each line by "--":

  private static String removeInLineSQLComments(String sql) {
      StringBuilder stringBuilder = new StringBuilder();
      for (String line : sql.split("\n")) {
          stringBuilder.append(line.split("--")[0]).append("\n");
      }
      return stringBuilder.toString();
  }

Upvotes: 1

Aubin
Aubin

Reputation: 14853

Regexp should be: --.*$, to match end of line in a portable way.

Upvotes: 1

Joop Eggen
Joop Eggen

Reputation: 109547

Pattern looks okay. Matcher is used as:

Pattern pattern = Pattern.compile("^(([^']+|'[^']*')*)--[^\r\n]*");
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
    matcher.appendReplacement(sb, "$1");
}
matcher.appendTail(sb);
return sb.toString();

The pattern does:

^((
    [^']+
|
    '[^']*'
)*)
--[^\r\n]*

Line start, repetition of either non-apostrophe chars or string literal. The extra parenthesis is to have $1 take the remaining SQL.

Upvotes: 0

Related Questions