Whats the correct way to match a filename from a full path using string-match-p
.
Say I want to check if the file is a git commit message COMMIT_EDITMSG
or svn-commit.tmp
or svn-commit.3.tmp
to pass to 3rd party code that only takes regular expressions (something like auto-mode-alist
).
eg: (string-match my-clever-regex "/path/to/svn-commit.3.tmp")
Using the COMMIT_EDITMSG
as an example:
"COMMIT_EDITMSG\\'"
also matchesPREFIX_COMMIT_EDITMSG
."/COMMIT_EDITMSG\\'"
fails to matchCOMMIT_EDITMSG
or\COMMIT_EDITMSG
.
How can I match a filename which may or may not contain a path?
grep
,re-search-forward
? Emacs regexp or regexp forfind
or something else? Define "correct". Please clarify what you're trying to do. – Drew Jan 28 '20 at 1:36buffer-file-name
, that will have a directory component to it, in which case"/COMMIT_EDITMSG\\'"
may be all you need for a regexp. – phils Jan 28 '20 at 6:37\
on WIN32 because I've no idea where this value is coming from; but if it's coming from Emacs then you may find that it's/
even on WIN32. I suggest either testing this yourself, or else giving details so that someone else would be able to do so. – phils Jan 28 '20 at 8:07\'
because that's the correct way to say "end of text". Whilst not recommended, newlines are valid in filenames on most systems, so$
is potentially insufficient. For more examples see virtually every entry inauto-mode-alist
. – phils Jan 28 '20 at 8:12