Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!ucsd!nosc!humu!uhccux!julian From: julian@uhccux.uhcc.hawaii.edu (Julian Cowley) Newsgroups: comp.emacs Subject: Re: Lisp Programming: Substring Searches, etc. Message-ID: <4307@uhccux.uhcc.hawaii.edu> Date: 10 Jul 89 01:42:59 GMT References: <397@lxn.eds.com> <65891@yale-celray.yale.UUCP> Organization: University of Hawaii at Manoa Lines: 36 In article spencer@eecs.umich.edu (Spencer W. Thomas) writes: >In article <65891@yale-celray.yale.UUCP> Duchier-Denys@cs.yale.edu (Denys Duchier) writes: > (defun extract-file-extension (filename) > (and (string-match ".+\\.\\(.*\\)$" filename) > (substring filename (match-beginning 1) (match-end 1)))) > >Actually, probably better would be > (string-match ".+\\.\\([^.]*\\)$" filename) >This pattern will match the last ".", while the given pattern will >match the first one. This has kind of regexp has come up before, I think. Two things need pointing out: 1) The above regexps incorrectly return the names of hidden files. E.g., (extract-file-extension "~/.letter") returns "letter". The correct result should be nil. 2) They also don't work with directories. E.g., (extract-file-extension "/usr/lib/sccs.help/cmds") returns "help/cmds". Again, the correct result should be nil. So, here is an improved regexp that correctly handles the above cases: (string-match "[^/]\\.\\([^/.]*\\)$" filename) julian@uhccux.uhcc.hawaii.edu | "Hawaii has always been a very pivotal role uunet!ucsd!nosc!uhccux!julian | in the Pacific. It is in the Pacific. julian@uhccux.bitnet | It is part of the United States that is an University of Hawaii at Manoa | island that is right here." - Quayle