需要帮助阅读以特定单词结尾的日志中的特定行
Need help to read specific line in a log which ends with specific word
我需要有关 java 代码的帮助以读取日志文件,该日志文件可以打印所有以 START 字结尾的行。
我的文件包含:
test 1 START
test2 XYZ
test 3 ABC
test 2 START
它应该打印
test 1 START
test 2 START
我试过下面的代码,但它只打印 START。
public class Findlog{
public static void main(String[] args) throws IOException {
BufferedReader r = new BufferedReader(new FileReader("myfile"));
Pattern patt = Pattern.compile(".*START$");
// For each line of input, try matching in it.
String line;
while ((line = r.readLine()) != null) {
// For each match in the line, extract and print it.
Matcher m = patt.matcher(line);
while (m.find()) {
// Simplest method:
// System.out.println(m.group(0));
// Get the starting position of the text
System.out.println(line.substring(line));
}
}
line.endsWith("START")
检查就足够了。这里不需要正则表达式。
我想您已经找到了解决方案。
无论如何,应该工作的正则表达式是:
".*START$"
表示:接受所有 (.*) 后跟 START,START 是行尾 ($)
public class Findlog{
public static void main(String[] args) throws IOException {
BufferedReader r = new BufferedReader(new FileReader("myfile"));
Pattern patt = Pattern.compile(".*START$");
// For each line of input, try matching in it.
String line;
while ((line = r.readLine()) != null) {
// For each match in the line, extract and print it.
Matcher m = patt.matcher(line);
while (m.find()) {
// Simplest method:
// System.out.println(m.group(0));
// Get the starting position of the text
System.out.println(line.substring(line));
}
}
您的代码的完整版本应如下所示
public class Findlog{
public static void main(String[] args) throws IOException {
BufferedReader r = new BufferedReader(new FileReader("myfile"));
String line;
while ((line = r.readLine()) != null) {
// For each match in the line, extract and print it.
if(line.endsWith("START"))
{
System.out.println(line);
}
}
如果您想跳过区分大小写,则代码应如下所示。
public class Findlog{
public static void main(String[] args) throws IOException {
BufferedReader r = new BufferedReader(new FileReader("myfile"));
String line;
while ((line = r.readLine()) != null) {
// For each match in the line, extract and print it.
if(line.toLowerCase().endsWith(matches.toLowerCase()))
{
System.out.println(line);
}
}
只需更改 if 条件。
我需要有关 java 代码的帮助以读取日志文件,该日志文件可以打印所有以 START 字结尾的行。
我的文件包含:
test 1 START
test2 XYZ
test 3 ABC
test 2 START
它应该打印
test 1 START
test 2 START
我试过下面的代码,但它只打印 START。
public class Findlog{
public static void main(String[] args) throws IOException {
BufferedReader r = new BufferedReader(new FileReader("myfile"));
Pattern patt = Pattern.compile(".*START$");
// For each line of input, try matching in it.
String line;
while ((line = r.readLine()) != null) {
// For each match in the line, extract and print it.
Matcher m = patt.matcher(line);
while (m.find()) {
// Simplest method:
// System.out.println(m.group(0));
// Get the starting position of the text
System.out.println(line.substring(line));
}
}
line.endsWith("START")
检查就足够了。这里不需要正则表达式。
我想您已经找到了解决方案。 无论如何,应该工作的正则表达式是:
".*START$"
表示:接受所有 (.*) 后跟 START,START 是行尾 ($)
public class Findlog{
public static void main(String[] args) throws IOException {
BufferedReader r = new BufferedReader(new FileReader("myfile"));
Pattern patt = Pattern.compile(".*START$");
// For each line of input, try matching in it.
String line;
while ((line = r.readLine()) != null) {
// For each match in the line, extract and print it.
Matcher m = patt.matcher(line);
while (m.find()) {
// Simplest method:
// System.out.println(m.group(0));
// Get the starting position of the text
System.out.println(line.substring(line));
}
}
您的代码的完整版本应如下所示
public class Findlog{
public static void main(String[] args) throws IOException {
BufferedReader r = new BufferedReader(new FileReader("myfile"));
String line;
while ((line = r.readLine()) != null) {
// For each match in the line, extract and print it.
if(line.endsWith("START"))
{
System.out.println(line);
}
}
如果您想跳过区分大小写,则代码应如下所示。
public class Findlog{
public static void main(String[] args) throws IOException {
BufferedReader r = new BufferedReader(new FileReader("myfile"));
String line;
while ((line = r.readLine()) != null) {
// For each match in the line, extract and print it.
if(line.toLowerCase().endsWith(matches.toLowerCase()))
{
System.out.println(line);
}
}
只需更改 if 条件。