0

I read lines of files to List

The contents of first file as follow:

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:]
2017-08-18 09:00:00 ready now [Contact:NO] [REF:123] [REF2:] [REF3:] [Name:BB] [Fam:bb] [TEMP:-2:0:-2:0:-2] [Resident:138:booking] [end:0:] 
2017-08-19 010:00:00 Comming in [Contact:NO] [REF:] [REF2:] [REF3:] [Name:+AA] [Fam:aa] [TEMP:-2:0:-2:0:-2] [Resident:9:not booking] [end:0:]
2017-08-10 11:00:00 ready now [Contact:NO] [REF:123] [REF2:] [REF3:] [Name:BB] [Fam:bb] [TEMP:-2:0:-2:0:-2] [Resident:138:free] [end:0:]

the contents of second file as follow:

2017-08-20 12:00:00 Comming in [Contact:NO] [REF:] [REF2:] [REF3:] [Name:+AA] [Fam:aa] [TEMP:-2:0:-2:0:-2] [Resident:9:booking] [end:0:] 
2017-08-21 13:00:00 ready now [Contact:NO] [REF:123] [REF2:] [REF3:] [Name:BB] [Fam:bb] [TEMP:-2:0:-2:0:-2] [Resident:138:booking] [end:0:] 
2017-08-22 14:00:00 ready now [Contact:NO] [REF:123] [REF2:] [REF3:] [Name:AA] [Fam:bb] [TEMP:-2:0:-2:0:-2] [Resident:130:free] [end:0:]
2017-08-23 15:00:00 Comming in [Contact:NO] [REF:] [REF2:] [REF3:] [Name:+BB] [Fam:aa] [TEMP:-2:0:-2:0:-2] [Resident:9:booking] [end:0:]
2017-08-24 16:00:00 ready now [Contact:NO] [REF:123] [REF2:] [REF3:] [Name:AA] [Fam:bb] [TEMP:-2:0:-2:0:-2] [Resident:138:booking] [end:0:]

I use code in java to search in files and fetch each line has that values

This the first element of myList(its the contents of first file)

[
[
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:], 
2017-08-18 09:00:00 ready now [Contact:NO] [REF:123] [REF2:] [REF3:] [Name:BB] [Fam:bb] [TEMP:-2:0:-2:0:-2] [Resident:138:booking] [end:0:], 
2017-08-19 010:00:00 Comming in [Contact:NO] [REF:] [REF2:] [REF3:] [Name:+AA] [Fam:aa] [TEMP:-2:0:-2:0:-2] [Resident:9:not booking] [end:0:],
2017-08-10 11:00:00 ready now [Contact:NO] [REF:123] [REF2:] [REF3:] [Name:BB] [Fam:bb] [TEMP:-2:0:-2:0:-2] [Resident:138:free] [end:0:]
],

and this the second element of myList(its the contents of second files)

[
2017-08-20 12:00:00 Comming in [Contact:NO] [REF:] [REF2:] [REF3:] [Name:+AA] [Fam:aa] [TEMP:-2:0:-2:0:-2] [Resident:9:booking] [end:0:], 
2017-08-21 13:00:00 ready now [Contact:NO] [REF:123] [REF2:] [REF3:] [Name:BB] [Fam:bb] [TEMP:-2:0:-2:0:-2] [Resident:138:booking] [end:0:], 
2017-08-22 14:00:00 ready now [Contact:NO] [REF:123] [REF2:] [REF3:] [Name:AA] [Fam:bb] [TEMP:-2:0:-2:0:-2] [Resident:130:free] [end:0:],
2017-08-23 15:00:00 Comming in [Contact:NO] [REF:] [REF2:] [REF3:] [Name:+BB] [Fam:aa] [TEMP:-2:0:-2:0:-2] [Resident:9:booking] [end:0:],
2017-08-24 16:00:00 ready now [Contact:NO] [REF:123] [REF2:] [REF3:] [Name:AA] [Fam:bb] [TEMP:-2:0:-2:0:-2] [Resident:138:booking] [end:0:]
]
]

I tried now to extract helpful info using this Regex from answer by Wiktor Stribiżew

^\[?(\d[\d: -]+\d).*?\[Name:(\w*)].*?\[Fam:(\w*)].*?\[Resident:\w*:([^\]]*)]

How to implement this regex in java to get each info: {note: the list some times is larger of that}

the date and time
the name 
the fam
the resident
Wasfy
  • 73
  • 8
  • Now, you show more data, and the regex must be [`^\[*(\d[\d: -]+\d).*?\[Name:([^\]]*)].*?\[Fam:(\w*)].*?\[Resident:\w*:([^\]]*)]`](https://regex101.com/r/bmFO6R/1) – Wiktor Stribiżew Aug 24 '17 at 18:26
  • @WiktorStribiżew that result of list when print it manually using java – Wasfy Aug 24 '17 at 18:28
  • Please @WiktorStribizew How to apply java on this regex – Wasfy Aug 24 '17 at 18:31
  • Maybe https://ideone.com/5k1i8m will be enough for you? – Wiktor Stribiżew Aug 24 '17 at 18:37
  • @WiktorStribiżew the cod you provide extract only first four group how to loop through that list and extract each for example using replaceAll(regex,"$1"). Are you can guide me please – Wasfy Aug 24 '17 at 19:00
  • `for (String s: lst) { .... }` – Wiktor Stribiżew Aug 24 '17 at 19:13
  • I try with this but List list = Arrays.asList(s); for (String str: list) { System.out.println("--"+str.replaceAll(regex, "$1")); } – Wasfy Aug 24 '17 at 19:24
  • I already showed: use `Matcher` / `Pattern`. https://ideone.com/tv8SEi – Wiktor Stribiżew Aug 24 '17 at 19:29
  • @WiktorStribiżew I updated my question – Wasfy Aug 25 '17 at 10:58
  • 1
    When reading files, you may parse the lines with regex at once, on the fly. Why read the lines into separate lists? I can't help further as I do not know what your final data structure should be. You may declare `List datetimes = new ArrayList<>();`, `List names = new ArrayList<>();`, `List fams = new ArrayList<>();` and `List residents = new ArrayList<>();` and add captured values there using `names.add(m.group(2));`... etc. – Wiktor Stribiżew Aug 25 '17 at 11:02
  • 1
    Yes @WiktorStribiżew that exactly what i expected thanks alot to your help. – Wasfy Aug 25 '17 at 11:30

0 Answers0