Jump to content

Extract file extension: Difference between revisions

→‎{{header|Perl 6}}: use the new test-cases, and compare output to the built-in .extension method
m (→‎{{header|REXX}}: changed comments and whitespace.)
(→‎{{header|Perl 6}}: use the new test-cases, and compare output to the built-in .extension method)
Line 10:
 
 
If your programming language (or standard library) has built-in functionality for extracting a filename extension, show how it would be used, and clearlyhow state howexactly its behavior differs from this specification.
 
{{task heading|Specification}}
Line 31:
|
|-
| <code>characterModelCharacterModel.3D3DS</code>
| <code>.3D3DS</code>
|
|-
Line 801:
=={{header|Perl 6}}==
 
The built-in <code>.IO.extension::Path</code> methodclass canhas bean used, but it..<code>.extension</code> method:
 
<lang perl6>say $path.IO.extension;</lang>
Contrary to this task's specification, it
* doesn't include the dot in the output
* doesn't restrict extensionsthe extension to letters and numbers.
 
Here's a custom implementation which does satisfy those requirements:
 
Here's a custom implementation which does satisfy thosethe task requirements:
<lang perl6>sub extension (Str $filename --> Str) {
 
$filename.match(/:i ['.' <+alpha-[_]>+]? $ /).Str
<lang perl6>sub extension (Str $filenamepath --> Str) {
$filenamepath.match(/:i ['.' <+alpha-[_]>+]? $ /).Str
}</lang>
 
Testing:
 
<lang perl6>sayprintf "$_%-35s %->11s {(extension%-12s\n", $_, extension($_).perl}", for$_.IO.extension.perl
for <
'mywebsite.com/picture/image.png',
'http://mywebsiteexample.com/picture/imagedownload.tar.png',gz
CharacterModel.3DS
'myuniquefile.longextension',
.desktop
'IAmAFileWithoutExtension',
document
'/path/to.my/file',
'filedocument.odd_one',txt_backup
/etc/pam.d/login
>;</lang>
 
{{out}}
<pre>
http://example.com/download.tar.gz ".gz" "gz"
mywebsite.com/picture/image.png -> ".png"
CharacterModel.3DS ".3DS" "3DS"
http://mywebsite.com/picture/image.png -> ".png"
.desktop ".desktop" "desktop"
myuniquefile.longextension -> ".longextension"
document "" ""
IAmAFileWithoutExtension -> ""
document.txt_backup "" "txt_backup"
/path/to.my/file -> ""
/etc/pam.d/login "" ""
file.odd_one -> ""
</pre>
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.