fork download
  1. public class Main {
  2. public static void main(String[] args) {
  3. // Test strings
  4. String[] wholeStringArr = { "The time is 7:00.", "The time is 7:00:05.", "The time is 7:00:05.1.",
  5. "The time is 7:00:05.123.", "The time is 7:00:05.123456789." };
  6.  
  7. for (String wholeString : wholeStringArr) {
  8. String timeOnlyString = wholeString.replaceAll(".*?(\\d{1,2}:\\d{1,2}(?:\\:\\d{1,2}(?:\\.\\d{1,9})?)?).*",
  9. "$1");
  10. System.out.println(timeOnlyString);
  11. }
  12. }
  13. }
Success #stdin #stdout 0.08s 50268KB
stdin
Standard input is empty
stdout
7:00
7:00:05
7:00:05.1
7:00:05.123
7:00:05.123456789