对于(x,y)给定的范围,我如何返回x,y中的数字列表

发布于 2025-01-21 03:38:50 字数 914 浏览 1 评论 0原文

我有一个名为Posture0 Posture 1 posture 2等的文件列表,最高为4000。我想使用一个范围的函数,并为其围绕它提供文件模式。之所以尝试此操作的原因,我可能想获得某些文件范围(1-300),(2-219)以实现这一目标,我到目前为止尝试了这些文件:

自动化regex搜索模式。

我找到了自动化模式搜索的模块RGXG。对于0-180,它产生了此结果。

(180|1[0-7][0-9]|0?[0-9]{1,2})

所以这是我的正则。

posture+(180|1[0-7][0-9]|0[0-9]{1,2})+.jpg

这是不起作用的,因为它应该得到诸如Posture0和Posture180.jpg之类的文件。尽管丢失了0-100,但它也得到了它们,并且在此正则表达式中也发现了1000之类的图案。

后来,我在Python代码上运行了此模式。

import re

rootdir = "postures"

regex = re.compile('posture+(180|1[0-7][0-9]|0[0-9]{1,2})+.jpg')

for root, dirs, files in os.walk(rootdir):
  for file in files:
    if regex.match(file):
       print(file)

它返回文件,但也返回文件0-1500,并且不会在0-99之间返回数字。

我也搜索了Glob,但似乎我找不到这样的功能。

编辑:由于我的问题的反馈不清楚。我会尝试澄清它。

问题: 有没有办法自动化正则搜索字符串?自从我提到的案件没有被捕获以来,我尝试过的那个对我来说并不好。 谢谢 :)

I have got a list of files named posture0 posture 1 posture 2 etc up to 4000. I want to use a function that takes this range and gives me the file pattern around it. The reason why I tried this I might want to get certain ranges of files (1-300),(2-219) In order to achieve this I have tried these so far:

Automate a regex search pattern.

I have found the module rgxg which does automate the pattern searching. For 0-180 it generated this result.

(180|1[0-7][0-9]|0?[0-9]{1,2})

So this was my regex.

posture+(180|1[0-7][0-9]|0[0-9]{1,2})+.jpg

This didn't work because it should have gotten files like posture0 and to posture180.jpg. It got them although 0-100 was missing and also patterns like 1000 were also found with this regex.

And later on I ran this pattern on a python code.

import re

rootdir = "postures"

regex = re.compile('posture+(180|1[0-7][0-9]|0[0-9]{1,2})+.jpg')

for root, dirs, files in os.walk(rootdir):
  for file in files:
    if regex.match(file):
       print(file)

It returns files but it also returns file 0-1500 and it doesn't return numbers between 0-99.

I have also searched glob but it seems I can't find such functionality on it.

Edit:Since I have had the feedback of my question being not clear.I will try to clarify it.

The Question:
Is there a way to automate the search strings in regex? The one I have tried didn't work well for me since the cases I mentioned weren't captured.
Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

命硬 2025-01-28 03:38:50

这是我认为您的问题的解决方案。它实现了通往目录,较低值和上值的路径的函数,然后返回包含由下层和上值定义的范围内的所有文件名。

import os
import re

def get_filenames_in_range(path_to_directory, lower_bound, upper_bound):
    files = []
    # Iterate through files in current directory
    for f in os.listdir(path_to_directory):
        # os.listdir captures directories as well as files
        # so filter just for files
        if os.path.isfile(f):
            # Perform a regex match for numbers
            match = re.findall(r'\d+', f)
            # If numbers are found, check whether they are within range
            if match:
                number = int(match[0])
                if number >= lower_bound and number <= upper_bound:
                    files.append(f)
    return files

Here's a solution to what I think is your question. It implements a function that takes a path to a directory, a lower value and an upper value, then returns all filenames that contain the number within range defined by the lower and upper values.

import os
import re

def get_filenames_in_range(path_to_directory, lower_bound, upper_bound):
    files = []
    # Iterate through files in current directory
    for f in os.listdir(path_to_directory):
        # os.listdir captures directories as well as files
        # so filter just for files
        if os.path.isfile(f):
            # Perform a regex match for numbers
            match = re.findall(r'\d+', f)
            # If numbers are found, check whether they are within range
            if match:
                number = int(match[0])
                if number >= lower_bound and number <= upper_bound:
                    files.append(f)
    return files
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文