Java读取文件行并仅提取有用的信息 [英] Java-read lines of files and extract only the helpfull info

查看:140
本文介绍了Java读取文件行并仅提取有用的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有文件file1,file2包含以下内容:

I have files file1, file2 contains contents such:

[2017-02-01 10:00:00开始运行[错误:是] [做:不] [完成:] [提醒:] [闹钟号:123456789] [logno:123456789] [参考:-1:2:-1:-1:-1] [类型:2:大问题发生] [flag:0:]] < ---此line1

[2017-02-01 10:00:00 start running [error:yes] [doing:no] [finish:] [remind:] [alarmno:123456789] [logno:123456789] [ref:-1:2:-1:-1:-1] [type:2:big issues happen] [flag:0:]]<--- this line1

Line2:
除日期外,同一行1, type,logno和alarmno有时包含+或 - 符号。
...其他行
我已经读取了所有这些行到字符串 myLines 的列表。
(注意:file1的内容将是由逗号分隔的myLines的第一个元素,myLines的第二个元素将是由逗号分隔的file2的内容,依此类推。
例如myLines列表的第一个元素:

Line2: The same line 1 except date, type, logno and alarmno sometimes contains + or - signs. ... The other lines I already read all those lines to list of string myLines. (Note: the contents of file1 will be the first element of myLines seperated by comma and the second element of myLines will be the contents of file2 seprated by comma and so on. For exmple this the first element of myLines list:

[2017-02-01 10:00:00 start running [error:yes] [doing:no] [finish:] [remind:] [alarmno:123456789] [logno:123456789] [ref:-1:2:-1:-1:-1] [type:2:big issues happen] [flag:0:],
2017-02-01 10:00:00 start running [error:yes] [doing:no] [finish:] [remind:] [alarmno:123456789] [logno:123456789] [ref:-1:2:-1:-1:-1] [type:2:big issues happen] [flag:0:]]

< ---这是myLines的第一个元素,列出了file1的内容
如果文件包含一行代表myLines列表的第一个元素只会在没有逗号分隔的情况下包含该行。
我只想要

<--- this the first element of myLines list its the contents of file1 If the file contains one line that mean the first element of myLines list will only contains that line only without comma seprated. I want only


  1. 日期第一行

  2. alarmno (只有数字号,而不是上面
    中的例外字:123456789)

  3. logno in上面一行(123456789)

  4. 类型例如在上面一行中出现以下文字(大
    问题发生)

  1. The date at the first of each lines
  2. The alarmno(only the digits no, not the word for exmample in the above line: 123456789)
  3. The logno in the above line (123456789)
  4. The type for example in the above line the following text (big issues happen)

我试过这个:

String regex = "\\d{2}:\\d{2}:\\d{2}\\s+\\w*\\s+\\w*\\s+\\[\\w*:\\w*]\\s+\\[\\w*:\\]\\s+\\[\\w*:\\]\\s+\\[\\w*:\\]";
String s=null;
for(int i=0; i<myLines.size(); i++)
   {
     s = myLines.get(i).replaceAll(regex," ");
   }

但结果是日期和闹钟号:12345 ......和其他行内容。
我甚至试图重复那个表达但不帮助我。
有没有办法在java中实现它?

But that result the date and the alarmno:12345... and the other line contents. I even tried to repeat that expression but not help me. Are there any way to implement that in java?

推荐答案

你可以使用

^\[?(\d[\d-]+).*?\[alarmno:(\w*)].*?\[logno:(\w*)].*?\[type:\w*:([^\]]*)]

请参阅正则表达式演示

详细信息


  • ^ - 字符串开头

  • \ [? - 可选 [

  • (\d [\d - ] +) - 第1组:数字和一个或多个数字或 - s

  • 。*? - 任何

  • \ [alarmno: - a [0]除了换行符之外的0+个字符
  • alarmno: substring
  • (\ w *) - 第2组:0+单词字符

  • ] - 文字]

  • 。*? - 任何除了换行符之外的0+个字符尽可能少

  • \ [logno: - 文字 [logno: substring

  • (\ w *) - 第3组:0+字符

  • ] - ]

  • 。*? - 除了换行符之外的任何0 +字符尽可能少

  • \ [类型: - 一个 [类型: substring

  • \ w * - 0+ word chars

  • - 冒号

  • ([^ \]] *) - 第4组:除以外的0+字符

  • ] - ]

  • ^ - start of string
  • \[? - an optional [
  • (\d[\d-]+) - Group 1: a digits and 1 or more digits or -s
  • .*? - any 0+ chars other than line break chars as few as possible
  • \[alarmno: - a [alarmno: substring
  • (\w*) - Group 2: 0+ word chars
  • ] - a literal ]
  • .*? - any 0+ chars other than line break chars as few as possible
  • \[logno: - a literal [logno: substring
  • (\w*) - Group 3: 0+ word chars
  • ] - a ]
  • .*? - any 0+ chars other than line break chars as few as possible
  • \[type: - a [type: substring
  • \w* - 0+ word chars
  • : - a colon
  • ([^\]]*) - Group 4: 0+ chars other than ]
  • ] - a ]

Java演示

String s = "[2017-08-17 08:00:00 Comming in [Contact:NO] [REF:] [REF2:] [REF3:] [Name:+AA] [Fam:aa] [TEMP:-2:0:-2:0:-2] [Resident:9:free] [end:0:]";
Pattern pat = Pattern.compile("^\\[*(\\d[\\d: -]+\\d).*?\\[Name:([^]]*)].*?\\[Fam:(\\w*)].*?\\[Resident:\\w*:([^]]*)]");
Matcher matcher = pat.matcher(s);
if (matcher.find()){
    System.out.println("Date: " + matcher.group(1));
    System.out.println("Name: " + matcher.group(2)); 
    System.out.println("Fam: " + matcher.group(3)); 
    System.out.println("Resident: " + matcher.group(4)); 
} 

输出:

Date: 2017-08-17 08:00:00
Name: +AA
Fam: aa
Resident: free

这篇关于Java读取文件行并仅提取有用的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆