用于从路径中提取文件名的正则表达式 [英] Regex for extracting filename from path

查看:93
本文介绍了用于从路径中提取文件名的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只需要从以下路径中提取文件名(无文件扩展名)....

I need to extract just the filename (no file extension) from the following path....

\\my-local-server\path\to\this_file may_contain-any&character.pdf

我尝试了几种方法,大多数基于 http://regexr.com?302m5 之类的东西,但不能完全确定到达那里

I've tried several things, most based off of something like http://regexr.com?302m5 but can't quite get there

推荐答案

^\\(.+\\)*(.+)\.(.+)$

这个正则表达式已经在这两个例子上进行了测试:

This regex has been tested on these two examples:

\var\www\www.example.com\index.php
\index.php

\var\www\www.example.com\index.php
\index.php

第一个块(.+\)*"匹配目录路径.
第二个块(.+)"匹配没有扩展名的文件名.
第三个块(.+)$"匹配扩展名.

First block "(.+\)*" matches directory path.
Second block "(.+)" matches file name without extension.
Third block "(.+)$" matches extension.

这篇关于用于从路径中提取文件名的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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