正则表达式,用于检测的base64 EN codeD字符串 [英] RegEx for detecting base64 encoded strings
问题描述
我需要检测与形式的 @的base64 (例如 @ VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw ==
)在我的应用程序。
I need to detect strings with the form @base64 (e.g. @VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==
) in my application.
该@必须在开始和进行的base64 EN codeD字符串的字符集是 AZ
, AZ
, 0-9
, +
, /
和 =
。将此时,相应的普通前presion公司检测到它们?
The @ has to be at the beginning and the charset for base64 encoded strings is a-z
, A-Z
, 0-9
, +
, /
and =
. Would be the appropiate regular expresion to detect them?
感谢
推荐答案
像这样的东西应该做的(不进行适当的长度检查!)
Something like this should do (does not check for proper length!):
^@[a-zA-Z0-9+/]+={0,2}$
任何的base64 EN codeD字符串的长度必须是4的倍数,因此额外的。
The length of any base64 encoded string must be a multiple of 4, hence the additional.
在这里看到,检查对适当长度的解决方案:<一href=\"http://stackoverflow.com/questions/475074/regex-to-parse-or-validate-base64-data/475217#475217\">RegEx解析或验证Base64编码数据
See here for a solution that checks against proper length: RegEx to parse or validate Base64 data
从链接的答案正则表达式的快速解释:
A quick explanation of the regex from the linked answer:
^@ #match "@" at beginning of string
(?:[A-Za-z0-9+/]{4})* #match any number of 4-letter blocks of the base64 char set
(?:
[A-Za-z0-9+/]{2}== #match 2-letter block of the base64 char set followed by "==", together forming a 4-letter block
| # or
[A-Za-z0-9+/]{3}= #match 3-letter block of the base64 char set followed by "=", together forming a 4-letter block
)?
$ #match end of string
这篇关于正则表达式,用于检测的base64 EN codeD字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!