Search code examples
regexbase64eml

Regex for EML Base64 block


Is it possible to use regex to match entire RFC1341 / RFC2045 Base64 blocks (up to 76 characters per line), not just individual lines?

The closest that I've managed to get is ^(?:[a-zA-Z0-9+\/=-]{1,76}[\r|\n|\r\n]?)+ but it only finds individual lines in Notepad++ and it selects stuff other than Base64 blocks too.

Examples:

Is this possible?


Solution

  • You may use this regex to grab all eml blocks:

    ^(?:[\w+/=-]{76}\R)+[\w+/=-]{1,76}
    

    RegEx Demo

    RegEx Details:

    • ^: Line start
    • (?:: Start non-capture group
      • [\w+/=-]{76}: Match 76 of allowed characters inside [...]
      • \R: Match line break of any kind (unicode)
    • )+: end non-capture group. Match 1 or more of this group.
    • [\w+/=-]{1,76}: Match 1 to 76 of allowed characters inside [...]