eygraber
11/16/2021, 4:00 AMScanner(myFile).findAll(myPattern)
that returns a `Sequence<String>`(I know I can call asSequence
on the returned Stream
but I'm wondering if there's a specific Kotlin API for that)?Paul Griffith
11/16/2021, 6:25 AMasSequence
on the stream. Unless someone has implemented a Scanner-like class for KMM, that's probably as good as you'll getRein T
11/16/2021, 7:14 AMfun Scanner.all(matching : Pattern) = findAll(matching).asSequence()
eygraber
11/16/2021, 8:26 AMStephan Schroeder
11/16/2021, 10:16 AMScanner
is also JVM-only, isn't it?
Here's the extension function that'll work on a line-sequence:
fun Sequence<String>.findMatches(pattern: String): Sequence<String> {
val regex = pattern.toRegex()
return this.flatMap{line->regex.findAll(line).map{match->match.value}}
}
Here the Playground with some testdata: https://pl.kotl.in/yoF7jx5pv
Now you only have to figure out how to convert your file into a Sequence<String>
.Joffrey
11/16/2021, 1:22 PMSequence<String>
as others mentioned, and on JVM you can simply use Path.useLines or File.useLines like this:
Path("yourpath").useLines { seq ->
// do something with the sequence of lines
}
// here the file is closed automatically
eygraber
11/16/2021, 4:33 PMJoffrey
11/16/2021, 4:36 PMeygraber
11/16/2021, 4:40 PM