Extract file extension: Difference between revisions

Content added Content deleted
(→‎{{header|J}}: Update for task changes)
(→‎{{header|jq}}: update to conform with changed requirements)
Line 749: Line 749:
=={{header|jq}}==
=={{header|jq}}==


The following definitions include 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.
In the first section, a version intended for jq version 1.4 is presented.
Line 767: Line 764:
rindex(".") as $ix
rindex(".") as $ix
| if $ix then .[1+$ix:] as $ext
| if $ix then .[1+$ix:] as $ext
| if $ext|alphanumeric then $ext # or ".\($ext)" if the period is wanted
| if $ext|alphanumeric then ".\($ext)" # include the period
else ""
else ""
end
end
Line 775: Line 772:
{{works with|jq|1.5}}
{{works with|jq|1.5}}
<lang jq>def file_extension:
<lang jq>def file_extension:
match( "\\.([a-zA-Z0-9]*$)" ) // false
(match( "(\\.[a-zA-Z0-9]*$)" ) | .captures[0].string)
| if . then .captures[0].string else "" end ;</lang>
// "" ;</lang>


'''Examples''':
'''Examples''':


Using either version above gives the same results.
Using either version above gives the same results.
<lang jq>"picture.jpg",
<lang jq>"http://example.com/download.tar.gz",
"CharacterModel.3DS",
"myuniquefile.longextension",
".desktop",
"http://mywebsite.com/picture/image.png",
"document",
"myuniquefile.longextension",
"document.txt_backup",
"IAmAFileWithoutExtension",
"/path/to.my/file",
"/etc/pam.d/login"
| "\(.) 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}}
{{out}}
<pre>http://example.com/download.tar.gz has extension: .gz
<lang sh>$ jq -r -n -f Extract_file_extension.jq
picture.jpg has extension: "jpg"
CharacterModel.3DS has extension: .3DS
myuniquefile.longextension has extension: "longextension"
.desktop has extension: .desktop
http://mywebsite.com/picture/image.png has extension: "png"
document has extension:
myuniquefile.longextension has extension: "longextension"
document.txt_backup has extension:
IAmAFileWithoutExtension has extension: ""
/etc/pam.d/login has extension:
</pre>
/path/to.my/file has extension: ""
file.odd_one has extension: ""</lang>


=={{header|Kotlin}}==
=={{header|Kotlin}}==