Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
105
rated 0 times [  107] [ 2]  / answers: 1 / hits: 25508  / 10 Years ago, mon, april 14, 2014, 12:00:00

Content-disposition header contains filename which can be easily extracted, but sometimes it contains double quotes, sometimes no quotes and there are probably some other variants too. Can someone write a regex which works in all the cases.



Content-Disposition: attachment; filename=content.txt


Here are some of the possible target strings:



attachment; filename=content.txt
attachment; filename*=UTF-8''filename.txt
attachment; filename=EURO rates; filename*=utf-8''%e2%82%ac%20rates
attachment; filename=omáèka.jpg
and some other combinations might also be there

More From » regex

 Answers
12

You could try something in this spirit:



filename[^;=n]*=(([']).*?2|[^;n]*)

filename # match filename, followed by
[^;=n]* # anything but a ;, a = or a newline
=
( # first capturing group
([']) # either single or double quote, put it in capturing group 2
.*? # anything up until the first...
2 # matching quote (single if we found single, double if we find double)
| # OR
[^;n]* # anything but a ; or a newline
)


Your filename is in the first capturing group: http://regex101.com/r/hJ7tS6


[#71476] Friday, April 11, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lidialyrick

Total Points: 737
Total Questions: 104
Total Answers: 89

Location: Andorra
Member since Sat, May 27, 2023
1 Year ago
lidialyrick questions
;