匹配数字时,我似乎没有在 python 的正则表达式中正确处理

I seem not to get it right in regex in python when matching digits

我有一个包含镜像文件名的项目的列表。我只想过滤仅包含数字的名称。我似乎没有得到它的权利。即使匹配一个数字似乎也不起作用。我做的不对吗?任何线索或建议将不胜感激。

代码示例:

import re

pattern = r"^\d.+[.]"
pattern2 = r'\d*'

a = ["1000.mp4", "test.mp4", "110082.mp4", "829873.m4a"]

for i in a:
    if re.match(i, pattern):
        print(i)

这应该有效:

pattern = r"\d+?.\w+"

或者,如果要捕获文件名:

pattern = r"(\d+?).\w+"

但如果您的文件名包含“.”,这将不起作用。

您似乎弄错了 re.match 函数的参数顺序。

这是我测试过的代码,可以满足您的要求:

# Regex explanation,
# ^ Indicates position at start of a line.
# \d+ Indicates any digit and can be N number of times. Digit is defined as [0-9].
# \. Indicates a literal .
# \w+ Indicates any word and can be N number of times. Word is defined as [a-zA-Z0-9_].
pattern = r'^\d+\.\w+'

files_list = ["1000.mp4", "test.mp4", "110082.mp4", "829873.m4a"]

for file in files_list:
    # The order of arguments for re.match should be, (pattern, string).
    if re.match(pattern, file):
        print(file)

输出:

1000.mp4
110082.mp4
829873.m4a

这是一个工作代码。您可以 fiddle 绕过正则表达式以获得更好的正则表达式字符串,以便在没有点字符的情况下进行匹配。

import re

pattern = re.compile(r"^[0-9]+.")

a = ["1000.mp4", "test.mp4", "110082.mp4", "829873.m4a"]

for i in a:
    match = re.match(pattern, i)
    if match:
        matched_string = match.group(0)
        string_without_dot = matched_string[0:len(matched_string)-1]
        print(string_without_dot)