Extract groups matched regex to array in scala

arrays, extract, regex, regex-group, scala

Solution

regex.findAllIn(line).subgroups.toArray

Problem

I got this problem. I have a ``` val line:String = "PE018201804527901" ``` that matches with this ``` regex : (.{2})(.{4})(.{9})(.{2}) ``` I need to extract each group from the regex to an Array. The result would be: ``` Array["PE", "0182","018045279","01"] ``` I try to do this regex: ``` val regex = """(.{2})(.{4})(.{9})(.{2})""".r val x= regex.findAllIn(line).toArray ``` but it doesn't work!

Original source