SQLから文字列リテラルを抜き出す


private static final Pattern pattern = Pattern.compile("'(?:[^\']|'')*'");

private List getStringLiterals(String sql) {
List literals = new ArrayList();
Matcher matcher = pattern.matcher(sql);

while (matcher.find())
literals.add(matcher.group());

return literals;
}

public void func() {
List literals = getStringLiterals("select * from foo where bar = 'あいうえお' and zoo = 'i don''t know.'");
System.out.print(literals);
}

バージョンによってはうまく動かないかも。