Emacs:替换文件扩展名

7

给定一个文件名,我想将文件扩展名替换为“.org”。

我决定使用replace-regexp-in-string函数:

(replace-regexp-in-string "\\(.*\\)\..*$" "\\1.org" "file.conserv.module")

输出:

"file.conserv.modul.org"

我认为它将返回"file.conserv.org",就像Perl程序中一样。
my $str = 'file.conserv.module';
$str =~ s/(.*)\..*$/$1.org/;
print $str;

我该如何获取“file.conserv.org”?
2个回答

13

这可以在没有任何正则表达式的情况下实现您想要的功能:

(concat (file-name-sans-extension "file.conserv.module") ".org")
如果您想同时删除路径:
(concat (file-name-base "file.conserv.module") ".org")

哟!这是一个更简单的答案——建议您选择它而不是正则表达式选项。 - Dan
2
我找到了一个更好的函数:file-name-sans-extension,然后改进了你的答案。 - user4035

4

在Emacs中,您需要使用两个反斜杠转义括号和.(并使用\'而不是$来匹配字符串的结尾):

(replace-regexp-in-string "\\(.*\\)\\..*\\'" "\\1.org" "file.conserv.module")

更多有关双重 \ 转义的信息,请查看 Emacs Elisp手册中关于\字符的部分说明


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接