0

I need to parse a imap response to get the base64 string of a file.

The string is something like that:

--------------A5B0A8B4F69F8BD959B758D0
Content-Type: application/pdf;
 name="Myfile.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
 filename="Myfile.pdf"

JVBERi0xLjQKJeLjz9MKMyAwIG9iago8PC9UeXBlL1hPYmplY3QvQ29sb3JT
ZWI0ODRmNDE1ZDE0YmIyZmU2YjAzZDMzNjU+PGU5MDFiZTMzY2FlOTY4ZDM2
NmFmOGNhOTUxNTE0Nzk0Pl0vSW5mbyAyMyAwIFIvU2l6ZSAyND4+CnN0YXJ0
eHJlZgoyMzg2NgolJUVPRgo=

--------------A5B0A8B4F69F8BD959B758D0--

I need to get anything after filename="Myfile.pdf" (possibly excluding space) and before new line after =

I am trying with this regexp without success:

(?<=filename="Myfile\.pdf")(.*)(?=\r\n)

Actually I am not sure if there are spaces or new lines chars in the base64 string, I do not think.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Giacomo M
  • 4,450
  • 7
  • 28
  • 57

2 Answers2

1

Try this regular expression:

(?<=filename="Myfile\.pdf")\s+((?:\S+\s)+\S+)=

Please check the demo.

Update:

This one includes also the last '='

(?<=filename="Myfile\.pdf")\s+((?:\S+\s)+\S+=)

Please check demo here.

marianc
  • 439
  • 1
  • 4
  • 13
1

You may use

filename="Myfile\.pdf"\s*(\S.*(?:\n\S.*)*)

See the regex demo. Details:

  • filename="Myfile\.pdf" - a filename="Myfile.pdf" string
  • \s* - 0 or more whitespaces
  • (\S.*(?:\n\S.*)*) - Group 1:
    • \S - a non-whitespace char
    • .* - the rest of the line
    • (?:\n\S.*)* - 0 or more lines that do not start with whitespace.

Dart/Flutter code to get the first match:

final base64str = '--------------A5B0A8B4F69F8BD959B758D0\nContent-Type: application/pdf;\n name="Myfile.pdf"\nContent-Transfer-Encoding: base64\nContent-Disposition: attachment;\n\nfilename="Myfile.pdf"\n\nJVBERi0xLjQKJeLjz9MKMyAwIG9iago8PC9UeXBlL1hPYmplY3QvQ29sb3JT\nZWI0ODRmNDE1ZDE0YmIyZmU2YjAzZDMzNjU+PGU5MDFiZTMzY2FlOTY4ZDM2\nNmFmOGNhOTUxNTE0Nzk0Pl0vSW5mbyAyMyAwIFIvU2l6ZSAyND4+CnN0YXJ0\neHJlZgoyMzg2NgolJUVPRgo=\n\n--------------A5B0A8B4F69F8BD959B758D0--';
final rx = RegExp(r'filename="Myfile\.pdf"\s*(\S.*(?:\n\S.*)*)');
final match = rx.firstMatch(base64str);
if (match != null) {
  print(match.group(1));
}

Output:

JVBERi0xLjQKJeLjz9MKMyAwIG9iago8PC9UeXBlL1hPYmplY3QvQ29sb3JT
ZWI0ODRmNDE1ZDE0YmIyZmU2YjAzZDMzNjU+PGU5MDFiZTMzY2FlOTY4ZDM2
NmFmOGNhOTUxNTE0Nzk0Pl0vSW5mbyAyMyAwIFIvU2l6ZSAyND4+CnN0YXJ0
eHJlZgoyMzg2NgolJUVPRgo=
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • It seems to work. what about if the name of the file is in a variable? – Giacomo M Aug 05 '20 at 16:01
  • @GiacomoM You mean you have `final filename = "Myfile.pdf"`? And you build the regex pattern dynamically? – Wiktor Stribiżew Aug 05 '20 at 16:03
  • yes. I am trying to concat regexp with the filename variable but I always get null match – Giacomo M Aug 05 '20 at 16:04
  • I solved, really thanks. I have to escape other characters if i concat the regexp with a variable – Giacomo M Aug 05 '20 at 16:13
  • @GiacomoM You might use a [regex escape function](https://stackoverflow.com/a/50371136/3832970) to escape any arbitrary string. Then, just use `final word = 'Myfile\.pdf'; final rx = RegExp('filename="${escape(word)}"\\s*(\\S.*(?:\n\\S.*)*)');` – Wiktor Stribiżew Aug 05 '20 at 16:49