Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用python中的正则表达式匹配文件名_Python_Regex - Fatal编程技术网

用python中的正则表达式匹配文件名

用python中的正则表达式匹配文件名,python,regex,Python,Regex,我正在寻找一个正则表达式命令来匹配文件夹中的文件名。我已经得到了列表中的所有文件名。现在我想匹配循环中的模式(file是要匹配的字符串): 与: match = re.search(r'./{([\w]+)}_word1_{([0-9.]+)}_{([0-9.]+)}_{([0-9.]+)}*',file) 我曾经让正则表达式工作,但在这个特殊情况下,它简单不工作。你能帮我吗 我想通过以下方式继续匹配regex(我在这里写下了结果): 花括号是我的错。它们毫无意义。对不起 致以最良好的祝愿,

我正在寻找一个正则表达式命令来匹配文件夹中的文件名。我已经得到了列表中的所有文件名。现在我想匹配循环中的模式(file是要匹配的字符串):

与:

match = re.search(r'./{([\w]+)}_word1_{([0-9.]+)}_{([0-9.]+)}_{([0-9.]+)}*',file)
我曾经让正则表达式工作,但在这个特殊情况下,它简单不工作。你能帮我吗

我想通过以下方式继续匹配regex(我在这里写下了结果):

花括号是我的错。它们毫无意义。对不起

致以最良好的祝愿, 塞巴斯蒂安

您可以使用

r'\./([^\W_]+)_word1_([0-9.]+)_([0-9.]+)_([0-9]+(?:\.[0-9]+)*)'

详细信息

  • \。
    -文字点(如果未设置分界符,则与除换行符以外的任何字符匹配)
  • /
    -一个
    /
    符号(无需在Python正则表达式模式中转义)
  • ([^\W_]+)
    -第1组匹配1个或多个字母或数字(如果要匹配包含
    的块,请保留原始
    (\W+
    模式)
  • \u word1\u
    -文字子字符串
  • ([0-9.]+)
    -第1组匹配1个或多个数字和/或
    符号
  • -下划线
  • ([0-9.]+)
    -第2组匹配1个或多个数字和/或
    符号
  • -下划线
  • ([0-9]+(?:\[0-9]+)*)
    -第3组匹配1个或多个数字,然后匹配0+个
    序列和1个或多个数字
:

输出:

Part1: test1
Part2: 1.1
Part3: 1.2
Part4: 1.3
由于test_word.csv是文件名,其中的内容将始终更改,并且是以点分隔的数字,您可以尝试一下吗

r“test1_单词[_0-9.]*.csv”g

示例代码和测试字符串

# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r"test1_word[_0-9.]*.csv"

test_str = ("./test1_word1_1.1_1.2_1.3.csv\n"
    "./test1_word1_1.31.2_1.555.csv\n"
    "./test1_word1_10.31.2_2000.00.csv")

matches = re.finditer(regex, test_str)

for matchNum, match in enumerate(matches):
    matchNum = matchNum + 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.

想测试吗?会帮你的。

你说的“不起作用”是什么意思?请发布错误消息或所需的错误输出。请重试。你图案中的花括号是什么?大括号是有意义的,在模式中被视为文字符号。另外,
\w
也与
\u
匹配,因此,将
\w
替换为
[^\w\u]
-不确定它是否是您需要的。匹配文件名的规则是什么?问题是,我得到了一个none作为匹配项,因此regex无法识别该模式。打印匹配>>>无我想继续使用文件名中的表达式。我想要的输出是:match[0]=test1 match[1]=1.1 match[2]=1.2 match[3]=1.3请指定文件名匹配规则和示例字符串的预期输出。我刚刚注释了这个问题,以明确我期望的输出是什么。卷曲的括号确实完全没有意义,非常感谢。现在存在一个现有的匹配对象。我仍然无法访问它,但我会尝试在我自己的工作<代码>类型错误:“\u sre.sre\u Match”对象不可调用我将添加一个snippet@sebastian我不得不调整正则表达式以捕获最后一个没有尾随的组,修复了解释并添加了一个片段。@Wiktir Stribiżew:我接受了答案。我不想用多余的点来打扰你,所以我决定在这一点上取消比赛。我很高兴还有别的办法!非常感谢。不过,我想,你已经从“Wiktor Stribiżew”那里得到了答案。我犯了同样的错误,你没有以这种方式捕获不同的数字。因此需要多个捕获组
import re
rx = r"\./([^\W_]+)_word1_([0-9.]+)_([0-9.]+)_([0-9]+(?:\.[0-9]+)*)"
s = "./test1_word1_1.1_1.2_1.3.csv"
m = re.search(rx, s)
if m:
    print("Part1: {}\nPart2: {}\nPart3: {}\nPart4: {}".format(m.group(1), m.group(2), m.group(3), m.group(4) ))
Part1: test1
Part2: 1.1
Part3: 1.2
Part4: 1.3
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r"test1_word[_0-9.]*.csv"

test_str = ("./test1_word1_1.1_1.2_1.3.csv\n"
    "./test1_word1_1.31.2_1.555.csv\n"
    "./test1_word1_10.31.2_2000.00.csv")

matches = re.finditer(regex, test_str)

for matchNum, match in enumerate(matches):
    matchNum = matchNum + 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.