Skip to content

Instantly share code, notes, and snippets.

@chbaranowski
Created June 14, 2010 21:39
Show Gist options
  • Save chbaranowski/438345 to your computer and use it in GitHub Desktop.
Save chbaranowski/438345 to your computer and use it in GitHub Desktop.
String dateText = "bla bla 08:11:21 bla bla";
// RegEx with comments in Java
String timeRegex =
".* # Any chars \n" +
"(\\d\\d) # The hour \n" +
":(\\d\\d): # The minutes \n" +
"(\\d\\d) # The seconds \n" +
".* # Any chars \n";
Pattern pattern = Pattern.compile(timeRegex, Pattern.COMMENTS);
Matcher matcher = pattern.matcher(dateText);
if(matcher.find()){
String hour = matcher.group(1);
String min = matcher.group(2);
String sec = matcher.group(3);
System.out.println("Time : " + hour + "-" + min + "-" + sec);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment