The scala.util.matching.Regex appears to only have a single split() method whose behavior is to extract the match and return only the non-matching segments of the input string:
val str = "Here is some stuff PAT and second token PAT and third token PAT and fourth"
val r = "PAT".r
r.split(str)
res14: Array[String] = Array("Here is some stuff ", " and second token ", "
and third token ", " and fourth")
So is there another approach commonly used to retain the tokens in the returned list?
Note: the splitting patterns I use for actual work are somewhat complicated and certainly not constants like the above example. Therefore, simply inserting alternating constant values (or zipping them) would not suffice.
Update Here is a more representative regex
val str = "Here is some stuff PAT and second token PAT and third token
or something else and fourth"
val r = "(PAT|something else)".r
r.split(str)
res14: Array[String] = Array("Here is some stuff ", " and second token ", "
and third token ", " and fourth")
val r = "((?<=PAT)|(?=PAT))".rcould help.