如何使用RE从Python的完整路径中提取文件名?
我正在尝试使用Python中的正则表达式从任意完整路径中获取文件名。现在,我有r“ /.*?$“
,但是当前在第一个/
。
之后与所有内容匹配 我想使其在最后一个/
之后与所有内容匹配,并认为?
可以通过尽可能少的匹配来做到这一点。 吗?
实际上只是尽快使其与RE匹配而不是寻找最小的匹配?文档中的语言使它看起来像后者,但行为暗示了前者。
如何制作将匹配文件名称的正则表达式,/或有其他方法可以做到这一点?
I'm trying to get just the file name from an arbitrary full path using regular expressions in python. Right now I have r"/.*?$"
, but that's currently matching everything after the first /
.
I want to make it match everything after the last /
instead, and thought that the ?
would do that by making match as little as possible. Does the ?
actually just make it close a re match as soon as it can rather than looking for the smallest possible match? The language in the documentation makes it seem like the latter, but the behavior suggests the former.
How do I make a regular expression that will match the file name, and/or is there another way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论