Pattern: Base-encoded data

“Base-encoded data” is one of the patterns that you can select on the Match panel. Use this pattern to match specific bytes or text encoded as base64, base32, or base16. You can also use this pattern to match a certain number of bytes encoded as base64, base32, or base16.

This example shows how you can use the “base-encoded data” pattern to make your regular expression match certain bytes encoded as base64. You can find this example as “Pattern: base-encoded data” in the RegexMagic library.

  1. Click the New Formula button on the top toolbar to clear out all settings on the Samples, Match, and Action panels.
  2. On the Samples panel, paste in one new sample:
    UmVnZXhNYWdpYw
    UmVnZXhNYWdpYw==
    UmVnZXhNYWdpYyByb2Nrcw==
    UmVnZXhNYWdpYyByb2NrcyE=
  3. On the Match panel, set both “begin regex match at” and “end regex match at” to “anywhere”.
  4. Click the Add First Field button to add field 1.
  5. In the “pattern to match field” drop-down list, select “base-encoded data”.



  6. Set “encoding” to “base64”. This is the encoding that the pattern will try to match.
  7. Set “encoded data” to “Unicode, UTF-8” so we can provide the text that we want the pattern to match.
  8. Enter “RegexMagic” as the text to encode.
  9. Set “padding” to “optional padding” to allow the pattern to match the base64 string with or without the trailing == used by base64 for padding.
  10. On the Regex panel, select “Python 3.13” as your application, turn off free-spacing, and turn off mode modifiers. Click the Generate button, and you’ll get this regular expression:
    UmVnZXhNYWdpYw(?:={2})?
    Required options: Case sensitive.
    Unused options: Exact spacing; Dot doesn’t match line breaks; ^$ don’t match at line breaks.
  11. The Samples panel now shows that this regular expression correctly matches the literal text we typed in:
    UmVnZXhNYWdpYw
    UmVnZXhNYWdpYw==
    UmVnZXhNYWdpYyByb2Nrcw==
    UmVnZXhNYWdpYyByb2NrcyE=

As demonstrated by this example, the key feature of the “base-encoded data” pattern is that you provide RegexMagic with the underlying text or bytes that you’re trying to search for, which RegexMagic then converts to base64, base32, or base16. In this example we provide “RegexMagic” as the text to match. RegexMagic first determines the bytes that represent the text in our chosen encoding (UTF-8). It then converts those bytes to base64, which in this case produces “UmVnZXhNYWdpYw”. The generated pattern then matches this text. If you already have the base64 representation of the data you’re looking for then you can use the “literal text” instead.

Note that base64 uses blocks of 4 characters to encode blocks of 3 bytes. If the text you want to match is not a multiple of 3 bytes then the missing bytes affect the encoding. In the sample text above, the first two lines encode “RegexMagic” without and with padding. The 3rd line encodes “RegexMagic rocks” and the 4th “RegexMagic rocks!”. Our regex does not match the 3rd and 4th lines even though they encode “RegexMagic” because “c” is encoded as “Yw” while “c r” is encoded as “YyBy”. If you want to search for shorter strings within longer base64 data you need to take this into account. We can modify the above example to make it match the start of all 4 lines:

  1. Change the text to encode to “RegexMagi”, minus the trailing “c”.
  2. The generated regex loses the trailing “Yw”. It also loses the padding because base64 does not use padding when encoding a multiple of 3 bytes. Essentially, we’re left wtih a regex that matches literal text:
    UmVnZXhNYWdp
    Required options: Case sensitive.
    Unused options: Exact spacing; Dot doesn’t match line breaks; ^$ don’t match at line breaks..
  3. The Samples panel now shows that this regular expression matches “RegexMagic” encoded as base64 at the start of all 4 lines:
    UmVnZXhNYWdpYw
    UmVnZXhNYWdpYw==
    UmVnZXhNYWdpYyByb2Nrcw==
    UmVnZXhNYWdpYyByb2NrcyE=

Related Examples

Reference