用于解析目录和文件名的正则表达式 [英] Regex for parsing directory and filename
问题描述
我正在尝试编写一个正则表达式,以使用匹配组来解析完全限定路径的目录和文件名.
I'm trying to write a regex that will parse out the directory and filename of a fully qualified path using matching groups.
所以...
/var/log/xyz/10032008.log
将识别group 1 to be "/var/log/xyz"
和group 2 to be "10032008.log"
看起来很简单,但我无法让匹配的小组在我的一生中工作.
Seems simple but I can't get the matching groups to work for the life of me.
注意:正如一些受访者所指出的那样,这可能不是正则表达式的好用法.通常,我更喜欢使用我使用的语言的文件API.我实际上想做的事情比这要复杂一些,但要解释起来要困难得多,所以我选择了每个人都熟悉的领域,以便最简洁地描述根本问题.
NOTE: As pointed out by some of the respondents this is probably not a good use of regular expressions. Generally I'd prefer to use the file API of the language I was using. What I'm actually trying to do is a little more complicated than this but would have been much more difficult to explain, so I chose a domain that everyone would be familiar with in order to most succinctly describe the root problem.
推荐答案
尝试一下:
^(.+)\/([^/]+)$
转义斜线转义以防止在复制/粘贴正则表达式时出现问题
escaped the forward slash to prevent problems when copy/pasting the Regex
这篇关于用于解析目录和文件名的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!