How to extract a valid email address from a larger string in Scala

My scala version 2.7.7

I am trying to extract an email address from a larger string. the string itself does not match the format. The code I have is:

import scala.util.matching.Regex import scala.util.matching._ val Reg = """\b[A-Z0-9._%+-] +@ [A-Z0-9.-]+\.[AZ]{2,4}\b""".r "yo my name is joe : joe@gmail.com " match { case Reg(e) => println("match: " + e) case _ => println("fail") } 

Regex goes into RegExBuilder but fails for scala. Also, if there is another way to do this without regular expression, that would be fine too. Thanks!

+4
source share
3 answers

As Alan Moore noted, you need to add (?i) to the beginning of the pattern to make it case insensitive. Also note that using Regex directly matches the entire string. If you want to find one in a larger line, you can call findFirstIn() or use one of the similar Regex methods.

 val reg = """(?i)\b[A-Z0-9._%+-] +@ [A-Z0-9.-]+\.[AZ]{2,4}\b""".r reg findFirstIn "yo my name is joe : joe@gmail.com " match { case Some(email) => println("match: " + email) case None => println("fail") } 
+6
source

It looks like you are trying to do a case insensitive search, but you did not specify it anywhere. Try adding (?i) to the beginning of the regular expression:

 """(?i)\b[A-Z0-9._%+-] +@ [A-Z0-9.-]+\.[AZ]{2,4}\b""".r 
+3
source

Well, ways to do this other than RE are probably much more merciless. The next step would probably be a parser of combinators. Lots of random codes for cutting strings would be even more general and almost certainly much more painful. In part, what suitable tactics depend on how fully (and how severely or softly) your recognizer should be. For example, the general form: Rudolf Reindeer < rudy.caribou@north _pole.rth> not accepted by your RE (even after weakening case sensitivity). The full-fledged parsing of RFC 2822 is quite complex for a RE-based approach.

+1
source

Source: https://habr.com/ru/post/1309933/


All Articles