Skip to content

Instantly share code, notes, and snippets.

@gre
Created November 15, 2012 13:43
Show Gist options
  • Save gre/4078718 to your computer and use it in GitHub Desktop.
Save gre/4078718 to your computer and use it in GitHub Desktop.
Easy way to decode a base64 in Scala
import org.apache.commons.codec.binary.Base64
val base64 = "data:([a-z]+);base64,(.*)".r
def decodeBase64 (src: String): Option[(String, Array[Byte])] = {
src match {
case base64(mimetype, data) => Some( (mimetype, Base64.decodeBase64(data.getBytes("utf-8"))) )
case _ => None
}
}
@cristiboariu
Copy link

Sorry but isn't your regex wrong?

Something like this:

"data":"data:image/jpeg;base64,/9j/4AAQSkZJRgABAgAAZABkAAD..."

is never caught,because "image/jpeg" is not matched by [a-z].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment