File extension is in extensions list: Difference between revisions

rewrite task description, to differentiate this task from Extract file extension
(rewrite task description, to differentiate this task from Extract file extension)
Line 1:
{{draft task}} [[Category:File_System_Operations]] [[Category:String_manipulation]]
Given a file name and a list of extensions (including the dot),
tell whether the file's extension is in the extensions list.
The check should be case insensitive.
The file might not have an extension.
 
[[wp:Filename extension|Filename extensions]] are a rudimentary but commonly used way of identifying files types.
 
{{task heading}}
 
Given an arbitrary filename and a list of extensions, tell whether the filename has one of those extensions.
 
Notes:
* The check should be case insensitive.
* The extension must occur at the very end of the filename, and be immediately preceded by a dot (<code>.</code>).
* You may assume that none of the given extensions are the empty string, and none of them contain a dot. Other than that they may be arbitrary strings.
 
 
;''Extra credit:''
: Allow extensions to contain dots. This way, users of your function/program have full control over what they consider as the extension in cases like <code>archive.tar.gz</code>.
: Please state clearly whether or not your solution does this.
 
{{task heading|Test cases}}
 
The following test cases all assume this list of extensions: &nbsp; <code>zip</code>, <code>rar</code>, <code>7z</code>, <code>gz</code>, <code>archive</code>, <code>A##</code>
 
{| class="wikitable"
|-
! Filename
! Result
|-
| <code>My_Data.a##</code> || true
|-
| <code>My_Data.tar.Gz</code> || true
|-
| <code>My_Data.gzip</code> || false
|-
| <code>My_Data.7z.backup</code> || false
|-
| <code>My_Data...</code> || false
|-
| <code>My_Data</code> || false
|}
 
If your solution does the extra credit requirement, add <code>tar.bz2</code> to the list of extensions, and check the following additional test cases:
 
{| class="wikitable"
|-
! Filename
! Result
|-
| <code>My_Data_v1.0.tar.bz2</code> || true
|-
| <code>My_Data_v1.0.bz2</code> || false
|}
 
{{task heading|Motivation}}
 
Checking if a file is in a certain category of file formats with known extensions (e.g. archive files, or image files) is a common problem in practice, and may be approached differently than extracting and outputting an arbitrary extension ''(see e.g. <code>FileNameExtensionFilter</code> in Java)''.
 
It also requires less assumptions about the format of an extension, because the calling code can decide what extensions are valid.
 
For these reasons, this task exists in addition to the [[Extract file extension]] task.
 
{{task heading|Related tasks}}
 
* [[Extract file extension]]
* [[String matching]]
 
<hr>
 
=={{header|AWK}}==
Anonymous user