英文:
how to write regex expression to find date and time
问题
我正在阅读一个文件,并在文件中寻找这个字符串。希望使用正则表达式捕获值(00:00:22)。我已经编写了正则表达式,但它没有找到那个值?
"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());
英文:
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());
答案1
得分: 0
@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));
}
}
英文:
@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));
}
}
答案2
得分: 0
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");
英文:
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");
答案3
得分: 0
- 使用正则表达式
\\d{1,2}:\\d{1,2}:\\d{1,2}
,表示三个由:
分隔的1到2位数字组合。 - 使用
m.find()
作为条件,使用m.group()
打印匹配的字符串。
示例:
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());
}
}
}
**输出:**
00:00:22
英文:
- Use the regex,
\\d{1,2}:\\d{1,2}:\\d{1,2}
which means three combinations of 1 or 2 digits separated by:
- Use
m.find()
as the condition andm.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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论