如何编写正则表达式以查找日期和时间。

huangapple go评论119阅读模式
英文:

how to write regex expression to find date and time

问题

我正在阅读一个文件,并在文件中寻找这个字符串。希望使用正则表达式捕获值(00:00:22)。我已经编写了正则表达式,但它没有找到那个值?

  1. "20.10.02 00:00:22:135 INFO Running Cron : BatchJob"
  2. Pattern p = Pattern.compile("([01][0-9]|2[0-3]):([01][0-9]|2[0-3]) INFO Running Cron", Pattern.CASE_INSENSITIVE);
  3. Matcher m = p.matcher(line);
  4. 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?

  1. "20.10.02 00:00:22:135 INFO Running Cron : BatchJob"
  2. Pattern p = Pattern.compile("([01][0-9]|2[0-3]):([01][0-9]|2[0-3]) INFO Running Cron", Pattern.CASE_INSENSITIVE);
  3. Matcher m = p.matcher(line);
  4. System.out.println(m.find());

答案1

得分: 0

  1. @Test
  2. void patternTest(){
  3. String line = "20.10.02 00:00:22:135 INFO Running Cron : BatchJob";
  4. Pattern p = Pattern.compile(".*(\\d\\d:\\d\\d:\\d\\d):.*");
  5. Matcher m = p.matcher(line);
  6. assertTrue(m.matches());
  7. System.out.println(m.group(1));
  8. }
  9. }
英文:
  1. @Test
  2. void patternTest(){
  3. String line = "20.10.02 00:00:22:135 INFO Running Cron : BatchJob";
  4. Pattern p = Pattern.compile(".*(\\d\\d:\\d\\d:\\d\\d):.*");
  5. Matcher m = p.matcher(line);
  6. assertTrue(m.matches());
  7. System.out.println(m.group(1));
  8. }
  9. }

答案2

得分: 0

  1. String str = "20.10.02 00:00:22:135 INFO Running Cron : BatchJob";
  2. Pattern pattern = Pattern.compile("(?<date>\\d{2}\\.\\d{2}\\.\\d{2})\\s+(?<time>\\d{2}:\\d{2}:\\d{2}:\\d{1,3})");
  3. Matcher matcher = pattern.matcher(str);
  4. if (matcher.find()) {
  5. System.out.println("date: " + matcher.group("date"));
  6. System.out.println("time: " + matcher.group("time"));
  7. } else
  8. System.err.println("date/time not found");
英文:

regex101.com

  1. String str = &quot;20.10.02 00:00:22:135 INFO Running Cron : BatchJob&quot;;
  2. Pattern pattern = Pattern.compile(&quot;(?&lt;date&gt;\\d{2}\\.\\d{2}\\.\\d{2})\\s+(?&lt;time&gt;\\d{2}:\\d{2}:\\d{2}:\\d{1,3})&quot;);
  3. Matcher matcher = pattern.matcher(str);
  4. if (matcher.find()) {
  5. System.out.println(&quot;date: &quot; + matcher.group(&quot;date&quot;));
  6. System.out.println(&quot;time: &quot; + matcher.group(&quot;time&quot;));
  7. } else
  8. System.err.println(&quot;date/time not found&quot;);

答案3

得分: 0

  1. 使用正则表达式 \\d{1,2}:\\d{1,2}:\\d{1,2},表示三个由 : 分隔的1到2位数字组合。
  2. 使用 m.find() 作为条件,使用 m.group() 打印匹配的字符串。

示例:

  1. import java.text.ParseException;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4. public class Main {
  5. public static void main(String[] args) throws ParseException {
  6. String line = "20.10.02 00:00:22:135 INFO Running Cron : BatchJob";
  7. Pattern p = Pattern.compile("\\d{1,2}:\\d{1,2}:\\d{1,2}");
  8. Matcher m = p.matcher(line);
  9. if (m.find()) {
  10. System.out.println(m.group());
  11. }
  12. }
  13. }
  14. **输出**

00:00:22

英文:
  1. Use the regex, \\d{1,2}:\\d{1,2}:\\d{1,2} which means three combinations of 1 or 2 digits separated by :
  2. Use m.find() as the condition and m.group() to print the matched string.

Demo:

  1. import java.text.ParseException;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4. public class Main {
  5. public static void main(String[] args) throws ParseException {
  6. String line = &quot;20.10.02 00:00:22:135 INFO Running Cron : BatchJob&quot;;
  7. Pattern p = Pattern.compile(&quot;\\d{1,2}:\\d{1,2}:\\d{1,2}&quot;);
  8. Matcher m = p.matcher(line);
  9. if (m.find()) {
  10. System.out.println(m.group());
  11. }
  12. }
  13. }

Output:

  1. 00:00:22

huangapple
  • 本文由 发表于 2020年10月20日 18:41:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64443461.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定