RegEx for detecting base64 encoded strings

后端 未结 3 1761
慢半拍i
慢半拍i 2021-02-07 16:40

I need to detect strings with the form @base64 (e.g. @VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==) in my application.

The @ has to be at the begin

3条回答
  •  春和景丽
    2021-02-07 17:26

    Here's an alternative regular expression:

    ^@(?=(.{4})*$)[A-Za-z0-9+/]*={0,2}$
    

    It satisfies the following conditions:

    • The string length after the @ sign must be a multiple of four - (?=^(.{4})*$)
    • The content must be alphanumeric characters or + or / - [A-Za-z0-9+/]*
    • It can have up to two padding (=) characters on the end - ={0,2}

提交回复
热议问题