Regex: Get substring before character pattern

For something more complex that does require a regex you can precede the regex of what you are looking for with a capture group:

val s = "all first text here!\nmore text here..."
val regex = Regex("^(.*)\n")
val match = regex.find(s)

println(match?.groups?.first()?.value)
1 Like