Created
November 15, 2012 13:43
-
-
Save gre/4078718 to your computer and use it in GitHub Desktop.
Easy way to decode a base64 in Scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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].