fork download
  1. import java.util.*
  2.  
  3. fun main(args: Array<String>) {
  4. data class Journal(val timestamp: String, val text: String)
  5. val timestampPattern = """\d{2}-\d{2}-\d{4}\h+\d{2}:\d{2}:\d{2}"""
  6. val journalRegex = """^($timestampPattern)\h*-\h*(.*(?:\R(?!$timestampPattern).*)*)""".toRegex(RegexOption.MULTILINE)
  7.  
  8. val journals = """
  9. 28-03-2020 23:00:00 - This
  10. is
  11. line
  12. 1
  13.  
  14. 28-03-2021 14:23:15 - This
  15. is
  16. line
  17. 2
  18.  
  19. """.trimIndent()
  20.  
  21. journalRegex.findAll(journals)
  22. .map { it.destructured }
  23. .map { (first, second) -> Journal(text = second, timestamp = first) }
  24. .forEachIndexed { index, journal -> println("Match #${index + 1}: $journal") }
  25. }
  26.  
Success #stdin #stdout 0.17s 44060KB
stdin
Standard input is empty
stdout
Match #1: Journal(timestamp=28-03-2020 23:00:00, text=This
is
line
1
)
Match #2: Journal(timestamp=28-03-2021 14:23:15, text=This
is
line
2
)