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}}== |
||
⚫ | |||
{{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 |
|||
⚫ | |||
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 |
| 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( "\\. |
(match( "(\\.[a-zA-Z0-9]*$)" ) | .captures[0].string) |
||
// "" ;</lang> |
|||
'''Examples''': |
'''Examples''': |
||
Using either version above gives the same results. |
Using either version above gives the same results. |
||
<lang jq>" |
<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", |
|||
"/ |
"/etc/pam.d/login" |
||
⚫ | |||
"file.odd_one" |
|||
⚫ | |||
⚫ | |||
{{out}} |
{{out}} |
||
<pre>http://example.com/download.tar.gz has extension: .gz |
|||
⚫ | |||
CharacterModel.3DS has extension: .3DS |
|||
.desktop has extension: .desktop |
|||
document has extension: |
|||
document.txt_backup 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}}== |