Search code examples
phpregexbase64

RegEx for detecting base64 encoded strings


I need to detect strings with the form @base64 (e.g. @VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==) in my application.
The @ has to be at the beginning and the charset for base64 encoded strings is a-z, A-Z, 0-9, +, / and =.

What would the appropriate regular expression to detect them be?


Solution

  • Something like this should do (does not check for proper length!):

    ^@[a-zA-Z0-9+/]+={,2}$
    

    The length of any base64 encoded string must be a multiple of 4, hence the additional.

    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