regex - Emacs:替换文件扩展名

标签 regex emacs elisp

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

我决定使用 replace-regexp-in-string功能:

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

输出:
"file.conserv.modul.org"

我想,它会像在 Perl 程序中一样返回“file.conserv.org”:
my $str = 'file.conserv.module';
$str =~ s/(.*)\..*$/$1.org/;
print $str;

我怎样才能得到“file.conserv.org”?

最佳答案

这可以在没有任何正则表达式的情况下执行您想要的操作:

(concat (file-name-sans-extension "file.conserv.module") ".org")

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

关于regex - Emacs:替换文件扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25204386/

相关文章:

regex - 正则表达式只匹配奇数或偶数

java - 使用 RegEx 忽略分隔符之前的特定字符

emacs - 使用 emacs 启动缓冲区,其行为类似于 Scratch

emacs - 在 Emacs 中禁用鼠标滚动

Emacs:键绑定(bind)到匿名函数的性能

emacs - emacs果酱:“无法打开加载文件”,“包”

regex - Elasticsearch中无法识别的字符转义

ruby-on-rails - Emacs ruby​​-mode 缩进私有(private)

emacs - Elisp 函数返回标记而不是正确的值

javascript - 正则表达式替换 float 中的多个句点