正则表达式删除文件的扩展名 [英] Regular expression to remove a file's extension

查看:170
本文介绍了正则表达式删除文件的扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个可以删除文件名扩展名的正则表达式,只返回文件名。

I am in need of a regular expression that can remove the extension of a filename, returning only the name of the file.

以下是一些输入和输出:

Here are some examples of inputs and outputs:

myfile.png     -> myfile
myfile.png.jpg -> myfile.png

我显然可以手动执行此操作(即从最后一个点移除所有内容)但是我我确定有一个正则表达式可以自己完成。

I can obviously do this manually (ie removing everything from the last dot) but I'm sure that there is a regular expression that can do this by itself.

只是为了记录,我在JavaScript中这样做

推荐答案

/(.*)\.[^.]+$/

结果将在第一个捕获组中。但是,在不使用正则表达式的情况下,找到最右边的位置然后将所有内容都放在它之前可能更有效。

Result will be in that first capture group. However, it's probably more efficient to just find the position of the rightmost period and then take everything before it, without using regex.

这篇关于正则表达式删除文件的扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆