Results 1 to 3 of 3
  1. #1
    spudillon is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default 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

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    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...)

    Java Code:
    		if (arg.matches(regexTS)) {
    			String[] tmpArr = arg.split("[,.:]");
    Or use a better/other regular expression then you could avoid the rest of your code (parseInt , compare and so on) :D
    Last edited by eRaaaa; 12-29-2010 at 03:01 PM.

  3. #3
    spudillon is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default

    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

  1. Regex: find all possible matches
    By Alessio in forum New To Java
    Replies: 3
    Last Post: 10-19-2010, 02:10 PM
  2. pattern matcher problem
    By shashanksworld in forum New To Java
    Replies: 1
    Last Post: 04-02-2010, 03:55 AM
  3. help: matches don`t work
    By asabdo in forum New To Java
    Replies: 6
    Last Post: 01-16-2010, 08:06 AM
  4. Regex Pattern/Matcher - Print only one occurrence!
    By racha0601 in forum Advanced Java
    Replies: 3
    Last Post: 04-06-2009, 06:05 PM
  5. Help with password matches
    By Albert in forum AWT / Swing
    Replies: 1
    Last Post: 07-10-2007, 05:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •