> For the complete documentation index, see [llms.txt](https://trizen.gitbook.io/sidef-lang/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://trizen.gitbook.io/sidef-lang/programming_tasks/e/extract_file_extension.md).

# Extract file extension

```ruby
func extension(filename) {
    filename.match(/(\.[a-z0-9]+)\z/i).to_s
}

var files = [
    'http://example.com/download.tar.gz',
    'CharacterModel.3DS',
    '.desktop',
    'document',
    'document.txt_backup',
    '/etc/pam.d/login',
]

files.each {|f|
    printf("%-36s -> %-11s\n", f.dump, extension(f).dump)
}
```

#### Output:

```
"http://example.com/download.tar.gz" -> ".gz"
"CharacterModel.3DS"                 -> ".3DS"
".desktop"                           -> ".desktop"
"document"                           -> ""
"document.txt_backup"                -> ""
"/etc/pam.d/login"                   -> ""
```
