I ran into such a problem, in the console I get this:

742 model 1 category 1 2218240 Kyiv 1447162980 src_ 2015-11-10 15 43 00 2200001 Darnytsya 

I only need to leave:

 742 Kyiv 2015-11-10 15:43:00 Darnytsya 

Who can help? How can I add this code?

 Pattern p = Pattern.compile("[A-Za-z0-9_-]+"); Matcher m = p.matcher(data); while(m.find()){System.out.print(data.substring(m.start(), m.end())+" ")} 
  • Why not use the usual split(" ") , and then pokleit necessary parts? - Nofate

2 answers 2

If you take a string that is, the code will be

 String string = "742 model 1 category 1 2218240 Kyiv 1447162980 src_ 2015-11-10 15 43 00 2200001 Darnytsya"; final Matcher matcher = Pattern.compile("(\\d+)\\s+.+?\\d{6}\\s+(\\w+).+?src_\\s(\\d{4}-\\d{2}-\\d{2}\\s\\d{2}\\s\\d{2}\\s\\d{2})\\s+\\d+\\s+(\\w+)").matcher(string); if (matcher.find()) { System.out.println(matcher.group(1)); System.out.println(matcher.group(2)); System.out.println(matcher.group(3).replace(' ', ':')); System.out.println(matcher.group(4)); } 

Out:

 742 Kyiv 2015-11-10:15:43:00 Darnytsya 

PS One line is very difficult to write a regular regular, but this regular is suitable for this line.

    Using the method String.split , split the output of the data console into separate fields.

     String[] fields = data.split("\\s+"); System.out.printf("%s %s %s %s:%s:%s %s", fields[0], fields[6], fields[9], fields[10], fields[11], fields[12], fields[14]); 
    • This would come up if every time the line has not changed. And the reason is that this line may be different every time. 742 model 1 category 1 2218240 Kyiv 1447162980 src_ 2015-11-10 15 43 00 2200001 Darnytsya . Next time it may be: 72 model 1 category 1 2218240 Dnipropetrovsk 1447162980 src_ 2015-11-10 15 43 00 2200001 Kyiv - Roman Kostetskyy
    • What other options are there? What is different about them? In the examples I see no fundamental differences. What is there, that there are 15 fields separated by spaces - Sergey
    • 742 1 1 2218240 Truskavets 1447162980 src_ 2015-11-10 15 43 00 2200001 Darnytsya 1447187400 src_ 2015-11-10 22 30 00 types title Seating first class letter u04211 places 156 title Seating second class letter u04212 places 349 and can be such a line 050 0 0 2218240 Truskavets 1447179000 src_ 2015-11-10 20 10 00 2200001 Kyiv-Pasazhyrsky 1447218900 src_ 2015-11-11 07 15 00 types title Suite first-class sleeper letter places 3 title Coupe coach with compartments letter u places 21 title Berth third-class sleeper letter u041f places 33 .. and I’m breaking my own govt - Roman Kostetskyy
    • Sorry, just misunderstood the topic problem :( - Roman Kostetskyy
    • Then see the answer of Andrew Bystrov. Can ^ add to the beginning of the expression - Sergey