如何检查字符串是否为连接的base64 codeD或不 [英] How to check whether the string is base64 encoded or not

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

问题描述

我要脱code为Base64 EN codeD字符串,然后将其存储在我的分贝。如果输入不带采用base64 code,则需要抛出一个错误。我如何检查是否字符串是的base64 ENO codeD?

I want to decode the base64 encoded string, then store it in my db. If the input does not base64 encode, need to throw an error. How can I check if the string was base64 enocoded?

推荐答案

您可以使用下面的正前pression检查一个字符串的base64 EN codeD或不:

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+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$

在base64编码,字符集是 [A-Z,A-Z,0-9和+ /] 。如果剩下的长度小于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 codeD或不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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