opensas
opensas

Reputation: 63415

How to extract email address from a text file with Scala

I'd like to know which is the simplest and most elegant way to process a text file with email addresses in it and extract them using Scala.

Upvotes: 1

Views: 915

Answers (1)

opensas
opensas

Reputation: 63415

Here is my own attempt at it:

scala> import scala.io.Source.fromFile

scala> val r = """(?i)\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b""".r

scala> fromFile("./mails").getLines.flatMap { r.findAllIn _ }.toList
res29: List[String] = List([email protected], [email protected], [email protected])

Source for the RegEx.

Upvotes: 3

Related Questions