Java 使用正则表达式匹配出包含23/07/06 13:07的行数
时间: 2023-08-02 13:06:39 浏览: 65
在Java中,可以使用正则表达式和`Pattern`类来匹配包含特定日期和时间的行数。下面是一个示例代码:
```java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
public static void main(String[] args) {
String input = "Some text\n" +
"23/07/06 13:07 - Line 1\n" +
"23/07/06 13:07 - Line 2\n" +
"Some more text\n" +
"23/07/06 13:08 - Line 3\n" +
"Line 4";
String pattern = "\\d{2}/\\d{2}/\\d{2} \\d{2}:\\d{2} - .*";
Pattern regex = Pattern.compile(pattern);
Matcher matcher = regex.matcher(input);
int lineCount = 0;
while (matcher.find()) {
lineCount++;
}
System.out.println("Number of lines with the specified date and time: " + lineCount);
}
}
```
在上面的示例中,我们使用正则表达式`\\d{2}/\\d{2}/\\d{2} \\d{2}:\\d{2} - .*`来匹配形如"23/07/06 13:07 - Line x"的行数。运行以上代码将输出匹配到的行数。