Results 1 to 3 of 3
- 12-29-2010, 01:15 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
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
- 12-29-2010, 01:55 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
both are different match operations so you cant compare them in my opinion.
Read Matcher (Java Platform SE 6)
But in your code, you don`t need the Matcher and Pattern class. You could use the matches method of the String class (String use Pattern and Matcher intern too...)
Or use a better/other regular expression then you could avoid the rest of your code (parseInt , compare and so on) :DJava Code:if (arg.matches(regexTS)) { String[] tmpArr = arg.split("[,.:]");Last edited by eRaaaa; 12-29-2010 at 02:01 PM.
- 12-30-2010, 02:37 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
Thanks for taking the time to check out my query.
->"both are different match operations so you cant compare them"
<-point taken there is no generic answer so i have opted for the code "as is above".
->"But in your code, you don`t need the Matcher and Pattern class"
<-compared to String.matches() this is costly but necessary as i need matcher.start()
as i need to read multiple logs and timestamps are not always hh:mm:ss.mls(nsnsnsns)
some are prefixed with date YYYYMMDD-hh:mm:ss.mls and need to enure hh<24,
mm<60 and so on.
***If it helps anyone in the future in this case i found Matcher.matches() was more efficient than Macther.find() and this result is completely dependant on the example.
Similar Threads
-
Regex: find all possible matches
By Alessio in forum New To JavaReplies: 3Last Post: 10-19-2010, 01:10 PM -
pattern matcher problem
By shashanksworld in forum New To JavaReplies: 1Last Post: 04-02-2010, 02:55 AM -
help: matches don`t work
By asabdo in forum New To JavaReplies: 6Last Post: 01-16-2010, 07:06 AM -
Regex Pattern/Matcher - Print only one occurrence!
By racha0601 in forum Advanced JavaReplies: 3Last Post: 04-06-2009, 05:05 PM -
Help with password matches
By Albert in forum AWT / SwingReplies: 1Last Post: 07-10-2007, 04:17 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks