如何检查字符串是否为Base64编码 [英] How to check whether a string is Base64 encoded or not

查看:321
本文介绍了如何检查字符串是否为Base64编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解码Base64编码的字符串,然后将其存储在我的数据库中.如果输入的内容不是Base64编码的,我需要抛出一个错误.

I want to decode a Base64 encoded string, then store it in my database. If the input is not Base64 encoded, I need to throw an error.

如何检查字符串是否经过Base64编码?

How can I check if a string is Base64 encoded?

推荐答案

您可以使用以下正则表达式来检查字符串是否经过base64编码:

You can use the following regular expression to check if a string is base64 encoded or not:

^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$

在base64编码中,字符集为[A-Z, a-z, 0-9, and + /].如果剩余长度小于4,则该字符串将用'='个字符填充.

In base64 encoding, the character set is [A-Z, a-z, 0-9, and + /]. If the rest length is less than 4, the string is padded with '=' characters.

^([A-Za-z0-9+/]{4})*表示字符串以0个或多个base64组开头.

^([A-Za-z0-9+/]{4})* means the string starts with 0 or more base64 groups.

([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$表示字符串以以下三种形式之一结尾:[A-Za-z0-9+/]{4}[A-Za-z0-9+/]{3}=[A-Za-z0-9+/]{2}==.

([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$ means the string ends in one of three forms: [A-Za-z0-9+/]{4}, [A-Za-z0-9+/]{3}= or [A-Za-z0-9+/]{2}==.

这篇关于如何检查字符串是否为Base64编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆