Jump to content

Extract file extension: Difference between revisions

→‎{{header|jq}}: update to conform with changed requirements
(→‎{{header|J}}: Update for task changes)
(→‎{{header|jq}}: update to conform with changed requirements)
Line 749:
=={{header|jq}}==
 
The following definitions excludeinclude the delimiting period.
{{update|jq|The format of a suffix has been clarified, and the test-cases have been replaced with new ones.}}
 
Pending resolution of the inconsistency in the task description as of this writing, the following
definitions exclude the delimiting period.
 
In the first section, a version intended for jq version 1.4 is presented.
Line 767 ⟶ 764:
rindex(".") as $ix
| if $ix then .[1+$ix:] as $ext
| if $ext|alphanumeric then $ext # or ".\($ext)" if# include the period is wanted
else ""
end
Line 775 ⟶ 772:
{{works with|jq|1.5}}
<lang jq>def file_extension:
(match( "(\\.([a-zA-Z0-9]*$)" ) //| .captures[0].string) false
| if . then .captures[0].string else// "" end ;</lang>
 
'''Examples''':
 
Using either version above gives the same results.
<lang jq>"picturehttp://example.jpgcom/download.tar.gz",
"CharacterModel.3DS",
"myuniquefile.longextension",
".desktop",
"http://mywebsite.com/picture/image.png",
"document",
"myuniquefile.longextension",
"document.txt_backup",
"IAmAFileWithoutExtension",
"/pathetc/topam.myd/filelogin",
| "\(.) has extension: \"\(file_extension)\""</lang>
"file.odd_one"
 
| "\(.) has extension: \"\(file_extension)\""</lang>
<lang sh>$ jq -r -n -f Extract_file_extension.jq</lang>
{{out}}
<pre>http://example.com/download.tar.gz has extension: .gz
<lang sh>$ jq -r -n -f Extract_file_extension.jq
pictureCharacterModel.jpg3DS has extension: "jpg".3DS
myuniquefile.longextensiondesktop has extension: "longextension".desktop
http://mywebsite.com/picture/image.pngdocument has extension: "png"
myuniquefiledocument.longextensiontxt_backup has extension: "longextension"
IAmAFileWithoutExtension/etc/pam.d/login has extension: ""
</pre>
/path/to.my/file has extension: ""
file.odd_one has extension: ""</lang>
 
=={{header|Kotlin}}==
2,507

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.