Aram Kirakosyan
Aram Kirakosyan

Reputation: 11

regular expressions in java matching first occurrence of time

I want to set a pattern which will find a time from line

String line = "INFO  00:08:39 - End executing test11093 : Next time will be Tue Nov 05 00:13:27 GMT 2013"
Pattern pTime = Pattern.compile("[0-9][0-9]:[0-9][0-9]:[0-9][0-9]");
      Matcher mTime = pTime.matcher(line);
      String dateTime = null;
      while (mTime.find())
      {
          dateTime = mTime.group();       
      }
      System.out.println(dateTime);

The output is : 00:13:27, but I want to get only the first time!

Upvotes: 1

Views: 59

Answers (2)

Abhishek Agarwal
Abhishek Agarwal

Reputation: 922

Use

if (mTime.find())

instead of:

while (mTime.find())

Upvotes: 0

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 135992

Try to add break; after dateTime = mTime.group(); or change the code this way

String dateTime = null;
if (mTime.find()) {
      dateTime = mTime.group();   
}

Upvotes: 2

Related Questions