0

I have files file1, file2 contains contents such:

[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: 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:]]

<--- 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. 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)

This what I tried:

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," ");
   }

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?

Wasfy
  • 73
  • 8
  • See https://regex101.com/r/qcO4wC/1 and a lite version, [`^\[?(\d[\d: -]+\d).*?\[alarmno:(\w*)].*?\[logno:(\w*)].*?\[type:([^\]]*)]`](https://regex101.com/r/qcO4wC/2) – Wiktor Stribiżew Jul 26 '17 at 19:43
  • @WiktorStribizew thanks for take that time for help i prefer the second but i add small change to match the digit and colon in typeno:digit: and match time as i don't want that. This is https://regex101.com/r/qcO4wC/3 – Wasfy Jul 27 '17 at 14:58
  • I wonderfull how you create such that regex and my modification was randomly with test for each modification i add. – Wasfy Jul 27 '17 at 15:03
  • What's best place to understand regex as i read http://ocpsoft.com/opensource/guide-to-regular-expressions-in-java-part-1/ – Wasfy Jul 27 '17 at 15:05
  • Sorry, does it mean I can post the `^\[?(\d[\d-]+).*?\[alarmno:(\w*)].*?\[logno:(\w*)].*?\[type:\w*:([^\]]*)]` as a solution for you to accept? – Wiktor Stribiżew Jul 27 '17 at 15:43
  • Yes @WiktorStribizew, your solution is very helpfull. – Wasfy Jul 27 '17 at 16:30

1 Answers1

1

You may use

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

See the regex demo

Details:

  • ^ - 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 demo:

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)); 
} 

Output:

Date: 2017-08-17 08:00:00
Name: +AA
Fam: aa
Resident: free
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
  • @Wasfy: I can only suggest doing all lessons at [regexone.com](http://regexone.com/), reading through [regular-expressions.info](http://www.regular-expressions.info), [regex SO tag description](http://stackoverflow.com/tags/regex/info) (with many other links to great online resources), and the community SO post called [What does the regex mean](http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean). A [rexegg.com](http://rexegg.com) is also a great site. – Wiktor Stribiżew Jul 27 '17 at 17:26
  • Thanks @WiktorStribizew. – Wasfy Jul 27 '17 at 18:11