I'm working on a Java which does some changes to an SQL file and covert it to an Oracle competible format.
It seems for changing the Timestamp\date values, I need to use an oracle function "To_TIMESTAMP" to change it to some oracle understandable formart.
Now to the real question. Below I have a String line;
(1,'Ctx_Log-Log','','08.03.2017','2017-03-08 10:59:31','10:59:32','2017-03-08 10:59:41')
where I'm interested in only values like '2017-03-08 10:59:31'
and '2017-03-08 10:59:41'
and these value can occur more than 2 times. I want to change\replace them to
To_TIMESTAMP('2017-03-08 10:59:31','YYYY-MM-DD HH24:MI:SS')
and To_TIMESTAMP('2017-03-08 10:59:41','YYYY-MM-DD HH24:MI:SS')
and I don't want to disturb the other values. I tried to regex solution, but I could only match and replace the start of the string, like
string.replaceAll(",'201", ",To_TIMESTAMP('201")
which replace it to To_TIMESTAMP('2017-03-08 10:59:31'
but I don't know how to replace the end part of '2017-03-08 10:59:31'
to '2017-03-08 10:59:31','YYYY-MM-DD HH24:MI:SS')
Any help will be appreciate. It doesn't matter which way or method is performed, as far as gives me the right solution. Thanks
A simple way to do the trick is:
public static void main(String[] args) {
String text = "(1,'Ctx_Log-Log','','08.03.2017','2017-03-08 10:59:31','10:59:32','2017-03-08 10:59:41')";
String regex = "'[0-9]{4}-[0-9]{2}-[0-9]{2}\\s[0-9]{2}:[0-9]{2}:[0-9]{2}'";
String template = "TO_TIMESTAMP(%s,'YYYY-MM-DD HH24:MI:SS')";
Matcher matcher = Pattern.compile(regex).matcher(text);
while (matcher.find()) {
String value = matcher.group();
text = text.replace(value, String.format(template, value));
}
System.out.println(text);
}