How to find whether a string is base64 encoded or not

Base64 encoding is a simple algorithm to translate text and binary streams into ASCII string format to be consumed by various programs as well as transmitted over the network.  Base64 is not an encryption technique rather its just a way to represent binary data in to string format. A typical base64 encoded string will look like this, ZW5jb2RlZCBzdHJpbmc= (Base64 encoding is generally achieved by splitting a stream or block of data into 6-bit fragments and interpreting each fragment as the position in the following series of characters. Therefore the encoding yields data selected from 64 possible characters).

Base64 Lookup Table

Base64 Lookup Table

Recently I came across a scenario in my grails project where we need to check whether the received string is base64 encoded or not and here is the solution I have used to solved the situation,


Note:  The programming language used here is grails and this regular expression can be re-used across any programming language.