Function: file-name-extension
file-name-extension is a byte-compiled function defined in
files.el.gz.
Signature
(file-name-extension FILENAME &optional PERIOD)
Documentation
Return FILENAME's final "extension" sans any backup version strings.
The extension, in a file name, is the part that begins with the last .,
except that a leading . of the file name, if there is one, doesn't count.
This function calls file-name-sans-versions, which see, to remove from
the extension it returns any parts that indicate backup versions and
version strings.
Return nil for extensionless file names such as foo.
Return the empty string for file names such as foo. that end in a period.
By default, the returned value excludes the period that starts the extension, but if the optional argument PERIOD is non-nil, the period is included in the value, and in that case, if FILENAME has no extension, the value is "".
Other relevant functions are documented in the file-name group.
Probably introduced at or before Emacs version 22.1.
Shortdoc
;; file-name
(file-name-extension "/tmp/foo.txt")
=> "txt"
Aliases
Source Code
;; Defined in /usr/src/emacs/lisp/files.el.gz
(defun file-name-extension (filename &optional period)
"Return FILENAME's final \"extension\" sans any backup version strings.
The extension, in a file name, is the part that begins with the last `.',
except that a leading `.' of the file name, if there is one, doesn't count.
This function calls `file-name-sans-versions', which see, to remove from
the extension it returns any parts that indicate backup versions and
version strings.
Return nil for extensionless file names such as `foo'.
Return the empty string for file names such as `foo.' that end in a period.
By default, the returned value excludes the period that starts the
extension, but if the optional argument PERIOD is non-nil, the period
is included in the value, and in that case, if FILENAME has no
extension, the value is \"\"."
(save-match-data
(let ((file (file-name-sans-versions (file-name-nondirectory filename))))
(if (and (string-match "\\.[^.]*\\'" file)
(not (eq 0 (match-beginning 0))))
(substring file (+ (match-beginning 0) (if period 0 1)))
(if period
"")))))