简体   繁体   中英

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!

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

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

Use

if (mTime.find())

instead of:

while (mTime.find())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM