I am reading a file and looking for this string in the file.looking to capture the value(00:00:22) using regex.I have written regular expression but its not finding that value?
"20.10.02 00:00:22:135 INFO Running Cron : BatchJob"
Pattern p = Pattern.compile("([01][0-9]|2[0-3]):([01][0-9]|2[0-3]) INFO Running Cron", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(line);
System.out.println(m.find());
@Test
void patternTest(){
String line = "20.10.02 00:00:22:135 INFO Running Cron : BatchJob";
Pattern p = Pattern.compile(".*(\\d\\d:\\d\\d:\\d\\d):.*");
Matcher m = p.matcher(line);
assertTrue(m.matches());
System.out.println(m.group(1));
}
}
String str = "20.10.02 00:00:22:135 INFO Running Cron : BatchJob";
Pattern pattern = Pattern.compile("(?<date>\\d{2}\\.\\d{2}\\.\\d{2})\\s+(?<time>\\d{2}:\\d{2}:\\d{2}:\\d{1,3})");
Matcher matcher = pattern.matcher(str);
if (matcher.find()) {
System.out.println("date: " + matcher.group("date"));
System.out.println("time: " + matcher.group("time"));
} else
System.err.println("date/time not found");
\\\\d{1,2}:\\\\d{1,2}:\\\\d{1,2}
which means three combinations of 1 or 2 digits separated by :
m.find()
as the condition and m.group()
to print the matched string.Demo:
import java.text.ParseException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) throws ParseException {
String line = "20.10.02 00:00:22:135 INFO Running Cron : BatchJob";
Pattern p = Pattern.compile("\\d{1,2}:\\d{1,2}:\\d{1,2}");
Matcher m = p.matcher(line);
if (m.find()) {
System.out.println(m.group());
}
}
}
Output:
00:00:22
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.