如何使用RE从Python的完整路径中提取文件名?

发布于 2025-02-08 13:01:46 字数 278 浏览 2 评论 0原文

我正在尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文