re = /^(((\r?\n|\s)*[A-Za-z0-9+\/]){4})*(((\r?\n|\s)*[A-Za-z0-9+\/]){2}(=(\r?\n|\s)*){2}|((\r?\n|\s)*[A-Za-z0-9+\/]){3}(=(\r?\n|\s)*))?$/m
str = '
This is a good test
to check for base64 encoded
strings because it forces
the string to begin and end
in its own line==
Arriba Cachipurriana
00000000: 4e 18 ac 8a c6 a0 a2 87 6d 7a cb 6d a1 c8 5e 72 :N.......mz.m..^r
00000010: 47 e8 ad b6 ac 7b ae 1e 9d ca 1d 79 db 2d ae 29 :G....{.....y.-.)
00000020: e0 b1 b7 9c 6a eb 1e 8a d7 e8 ad c7 ac b6 17 ac :....j...........
00000030: b6 b8 a7 82 da 1b 7a 08 a7 6a 77 5e 9d d8 a7 8a :......z..jw^....
00000040: db 28 c2 79 62 9d :.(.yb.
00000046.
--
CglUaGlzIGlzIGEgZ29vZCB0ZXN0IAp0byBjaGVjayBmb3IgYmFzZTY0IGVuY29kZWQgCnN0cmlu
Z3MgYmVjYXVzZSBpdCBmb3JjZXMgCnRoZSBzdHJpbmcgdG8gYmVnaW4gYW5kIGVuZCAKaW4gaXRz
IG93biBsaW5lPT0KQXJyaWJhIENhY2hpcHVycmlhbmEKMDAwMDAwMDA6IDRlIDE4IGFjIDhhIGM2
IGEwIGEyIDg3IDZkIDdhIGNiIDZkIGExIGM4IDVlIDcyIDpOLi4uLi4uLm16Lm0uLl5yCjAwMDAw
MDEwOiA0NyBlOCBhZCBiNiBhYyA3YiBhZSAxZSA5ZCBjYSAxZCA3OSBkYiAyZCBhZSAyOSA6Ry4u
Li57Li4uLi55Li0uKQowMDAwMDAyMDogZTAgYjEgYjcgOWMgNmEgZWIgMWUgOGEgZDcgZTggYWQg
YzcgYWMgYjYgMTcgYWMgOi4uLi5qLi4uLi4uLi4uLi4KMDAwMDAwMzA6IGI2IGI4IGE3IDgyIGRh
IDFiIDdhIDA4IGE3IDZhIDc3IDVlIDlkIGQ4IGE3IDhhIDouLi4uLi56Li5qd14uLi4uCjAwMDAw
MDQwOiBkYiAyOCBjMiA3OSA2MiA5ZCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICA6Ligu
eWIuCjAwMDAwMDQ2Cg==
'
subst = '<<<$0>>>'
result = str.gsub(re, subst)
# Print the result of the substitution
puts result
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Ruby, please visit: http://ruby-doc.org/core-2.2.0/Regexp.html