I'm trying to extract hours, minutes, seconds, and nanoseconds from a string time stamp in a log file.Here is the input string I am testing with:
SOME_TEXT,+09:30:01.040910105,SOME_TEXT,SOME_TEXT,SOME_TEXT
In Perl/Python, I would use the following regex to group the fields I am interested in:
(\d\d)\:(\d\d)\:(\d\d)\.(\d{9})
You can verify that the regex works with the test string at http://regexpal.com if you're curious.
So I tried to write a simple Java program that can extract the fields:
import java.util.regex.*;
public class Driver
{
static public void main(String[] args)
{
String t = new String("SOME_TEXT,+09:30:01.040910105,SOME_TEXT,SOME_TEXT,SOME_TEXT");
Pattern regex = Pattern.compile("(\\d\\d):(\\d\\d):(\\d\\d)\\.(\\d{9})");
Matcher matches = regex.matcher(t);
for (int i=1; i<matches.groupCount(); ++i)
{
System.out.println(matches.group(i));
}
}
}
My regex did not translate correctly, however. The following exception shows that it did not find any matches:
Exception in thread "main" java.lang.IllegalStateException: No match found
at java.util.regex.Matcher.group(Matcher.java:485)
at Driver.main(Driver.java:12)
How would I properly translate the regex from Perl/Python style to Java?