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

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

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");
英文:

regex101.com

String str = &quot;20.10.02 00:00:22:135 INFO Running Cron : BatchJob&quot;;
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;);
Matcher matcher = pattern.matcher(str);

if (matcher.find()) {
    System.out.println(&quot;date: &quot; + matcher.group(&quot;date&quot;));
    System.out.println(&quot;time: &quot; + matcher.group(&quot;time&quot;));
} else
    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() 打印匹配的字符串。

示例:

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

英文:
  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:

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 = &quot;20.10.02 00:00:22:135 INFO Running Cron : BatchJob&quot;;
		Pattern p = Pattern.compile(&quot;\\d{1,2}:\\d{1,2}:\\d{1,2}&quot;);
		Matcher m = p.matcher(line);

		if (m.find()) {
			System.out.println(m.group());
		}
	}
}

Output:

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:

确定