首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用Regex从字符串中获取文件名

如何使用Regex从字符串中获取文件名
EN

Stack Overflow用户
提问于 2019-06-14 01:28:18
回答 1查看 49关注 0票数 -1

我在这里有这个字符串:

代码语言:javascript
复制
"['\r\n                    File: FLO_JIUWASOKLDM_05_HetR_IUSJA_&_Cracks.mp4', <br/>, '\r\n                    Size: 48.14 MB                ']"

我有这个正则表达式\w+\.\w+

我想让正则表达式得到文件名FLO_JIUWASOKLDM_05_HetR_IUSJA_&_Cracks.mp4

但它在与号处中断,这将返回_Cracks.mp4,我需要做什么来修复它?我是Regex的新手。

EN

回答 1

Stack Overflow用户

发布于 2019-06-14 01:31:39

这里有许多选项可供选择,例如,其中一个选项:

代码语言:javascript
复制
([^\s]+\.[a-z][a-z0-9]+)

Demo

测试

代码语言:javascript
复制
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r"([^\s]+\.[a-z][a-z0-9]+)"

test_str = "\"['\\r\\n                    File: FLO_JIUWASOKLDM_05_HetR_IUSJA_&_Cracks.mp4', <br/>, '\\r\\n                    Size: 48.14 MB                ']\"
"

matches = re.finditer(regex, test_str, re.MULTILINE | re.IGNORECASE)

for matchNum, match in enumerate(matches, start=1):

    print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))

    for groupNum in range(0, len(match.groups())):
        groupNum = groupNum + 1

        print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))

# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56585758

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档