Matcher.matches() Vs. Matcher.find()
hey folks,
As the title suggests this is a performance issue i have with the Machter object. I am writing a simple script to pick the times of particular events in a log file. In my log processor class i have a method isTimeStamp() that performes a simple check to ensure i have picked up a timestamp and not some unfortunate list of digits.
String regexTS = "\\d{1,2}[.:]\\d{1,2}[.:]\\d{1,2}[.:,]\\d{3,}";
public static boolean isTimeStamp(String arg) throws NumberFormatException {
boolean timestamp = false;
Pattern pattern = Pattern.compile(regexTS);
Matcher matcher = pattern.matcher(arg);
if(matcher.matches()) { // or i can use if (matcher.find()){
int i = matcher.start();
String[] tmpArr = arg.substring(i).split("[,.:]");
try{
if (Integer.parseInt(tmpArr[0])<24 &
Integer.parseInt(tmpArr[1])<60 & Integer.parseInt(tmpArr[2])<60){
timestamp = true;
}
}catch (NumberFormatException e){
System.out.println("EOD_Report: NumberFormatException thrown:"
+" LogProcessor.isTimeStamp(): arg is "+arg);
}
In tests between the two options i have found the .matches() method to be more efficient can anyone please confirm this and if possible explain this please.
Thanks
Spudillon