Yes, you can do it with Scala Regex .
I found a regex on this site , feel free to use another if this doesn't suit you:
[-0-9a-zA-Z.+_] +@ [-0-9a-zA-Z.+_]+\.[a-zA-Z]{2,4}
The first thing we need to do is add parentheses around the groups:
([-0-9a-zA-Z.+_]+)@([-0-9a-zA-Z.+_]+)\.([a-zA-Z]{2,4})
With this, we have three groups: the part before @ , between @ and . and finally TLD.
Now we can create a Scala regex from it, and then use Scala to match the unapply pattern to get groups from Regex bound to variables:
val Email = """([-0-9a-zA-Z.+_]+)@([-0-9a-zA-Z.+_]+)\.([a-zA-Z]{2,4})""".r Email: scala.util.matching.Regex = ([-0-9a-zA-Z.+_]+)@([-0-9a-zA-Z.+_]+)\.([a-zA-Z] {2,4}) " user@domain.com " match { case Email(name, domain, zone) => println(name) println(domain) println(zone) }
source share