I wrote a search like this:
preparedStatement = connect.prepareStatement($sql$);
$code1$;
$resultSet$ = preparedStatement.executeQuery();
$code2$;
if ($resultSet$.next()) {
$code3$;
}
And a replace like this:
try (PreparedStatement preparedStatement = connect.prepareStatement($sql$)) {
$code1$;
try (ResultSet $resultSet$ = preparedStatement.executeQuery()) {
$code2$;
if ($resultSet$.next()) {
$code3$;
}
}
}
When I run this, it finds code that has an if { foo } else { bar}
structure. I'm fine with this, but the problem is it removes the else { bar }
part if I replace. I'd like this search/replace to work sensibly on if statements with an else, multiple else-ifs, or any mixture of these. Is there a way to do this with one search and replace?
I tried changing the if
in the search to have an else and that fixed the issue, but then it skips the code that only has an if without an else.