简体   繁体   English

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

[英]Matching filenames with regex in python

I'm looking for a regex command to match file names in a folder. 我正在寻找一个正则表达式命令来匹配文件夹中的文件名。 I already got all the filenames in a list. 我已经在列表中获得了所有文件名。 Now I want to match a pattern in a loop (file is the string to match): 现在我想匹配循环中的模式(文件是要匹配的字符串):

./test1_word1_1.1_1.2_1.3.csv

with: 有:

match = re.search(r'./{([\w]+)}_word1_{([0-9.]+)}_{([0-9.]+)}_{([0-9.]+)}*',file)

I used to get regex working but in this special case it simple doesn't work. 我曾经得到正则表达式工作,但在这种特殊情况下它很简单不起作用。 Can you help me with that? 你能帮帮我吗?

I want to continue with the match of regex the following way (I've written the outcome here): 我想以下面的方式继续正则表达式的匹配(我在这里写了结果):

match[0] = test1
match[1] = 1.1
match[2] = 1.2
match[3] = 1.3

The curly brackets are my fault. 大括号是我的错。 They don't make sense at all. 它们完全没有意义。 Sorry 抱歉

Best regards, sebastian 最好的问候,塞巴斯蒂安

You may use 你可以用

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

See the regex demo 请参阅正则表达式演示

Details : 细节

  • \\. - a literal dot (if it is unescaped it matches any char other than a line break char) - 一个文字点(如果它未转义,它匹配除了换行符之外的任何字符)
  • / - a / symbol (no need escaping it in a Python regex pattern) / - 一个/符号(不需要在Python正则表达式模式中转义它)
  • ([^\\W_]+) - Group 1 matching 1 or more letters or digits (if you want to match a chunk containing _ , keep your original (\\w+) pattern) ([^\\W_]+) - 组1匹配1个或多个字母或数字(如果要匹配包含_的块,保留原始(\\w+)模式)
  • _word1_ - a literal substring _word1_ - 文字子字符串
  • ([0-9.]+) - Group 1 matching 1 or more digits and/or . ([0-9.]+) - 组1匹配1位或更多位数和/或. symbols 符号
  • _ - an underscore _ - 下划线
  • ([0-9.]+) - Group 2 matching 1 or more digits and/or . ([0-9.]+) - 组2匹配1位或更多位数和/或. symbols 符号
  • _ - an underscore _ - 下划线
  • ([0-9]+(?:\\.[0-9]+)*) - Group 3 matching 1 or more digits, then 0+ sequences of a . ([0-9]+(?:\\.[0-9]+)*) - 组3匹配1个或多个数字,然后是([0-9]+(?:\\.[0-9]+)*)序列. and 1 or more digits 和1位或更多位数

Python demo : Python演示

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) ))

Output: 输出:

Part1: test1
Part2: 1.1
Part3: 1.2
Part4: 1.3

Since test_word<>.csv is the file name and content inside <> will always changing and are dot delimited numbers, Can you try this? 由于test_word <>。csv是文件名,<>内的内容将始终更改并且是点分隔的数字,您可以尝试这个吗?

r"test1_word[_0-9.]*.csv"g R “test1_word [_0-9。] *。CSV” 克

Sample code and test strings 示例代码和测试字符串

# 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.

Want to test? 想测试? https://regex101.com/ will help you. https://regex101.com/将帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM