Once whitespace is removed from the input, the following regex pattern will tell you whether the input is a valid base64-encoded string.
m{
^
(?: [A-Za-z0-9+/]{4} )*
(?:
[A-Za-z0-9+/]{2} [AEIMQUYcgkosw048] =
|
[A-Za-z0-9+/] [AQgw] ==
)?
\z
}x
As to whether the input was the result of bas64-encoding or not, one can't tell. This sentence would be found valid up to the final period.
$ perl -MMIME::Base64 -le'print decode_base64("This sentence would be
+found valid up to the final period")' | od -t x1
0000000 4e 18 ac b1 e9 ed 7a 77 1e c2 8b a5 75 b7 9f a2
0000020 e9 dd bd a9 62 76 ea 6d a2 d8 5e 7e 29 da 96 97
0000040 ab 8a 87 0a
0000044
Usually, one refers to the header associated with the content.
Update: Replaced /(?:|...|...)/ with /(?:...|...)?/
Update: Changed // to m{} to fix an unescaped / (as brought up in a reply).
|