简体   繁体   中英

Python regex equivalent of bash's {1..4}

Does python's regular expression have anything equivalent to match numbers in a given range?

For example in bash, you can match test19.txt, test20.txt, test21.txt by test{19..21}.txt

I am not looking for regular expression to match all digits like [1-2][0-9].

I want to match only a particular series of numbers starting from some number to another.

Update: The final aim is to create a regexp object with re.compile(), so that i can use it to search a big list of strings.

['text' + str(i) + '.txt' for i in range(19, 22)]

Will give you that list:

['test19.txt', 'test20.txt', 'test21.txt']

So you can list of the files that are in that list. For example if you have a list of words named words and want to filter those that match it:

r = ['text' + str(i) + '.txt' for i in range(19, 22)]
[x for x in words if x in r]

But if you really want a regexp:

re.compile('|'.join(['text' + str(i) + '.txt' for i in range(19, 22)]))

尽管还有另一个类似的问题( 正则表达式:Numeric Range ),其答案建议仅使用正则表达式来匹配\\d{1,3}沿线的数字出现,但该答案指向命令线工具rgxg ,可以生成与指定数字范围匹配的正则表达式。

Assume you have these files:

$ cd test
$ touch file{1..25}.txt
$ ls
file1.txt   file14.txt  file19.txt  file23.txt  file5.txt
file10.txt  file15.txt  file2.txt   file24.txt  file6.txt
file11.txt  file16.txt  file20.txt  file25.txt  file7.txt
file12.txt  file17.txt  file21.txt  file3.txt   file8.txt
file13.txt  file18.txt  file22.txt  file4.txt   file9.txt

You can use glob to match the grand pattern of file[numers].txt :

import glob
import os
import re

os.chdir('/Users/andrew/test')

print glob.glob('file[0-9]*.txt')
# ['file1.txt', 'file10.txt', 'file11.txt', 'file12.txt', 'file13.txt', 'file14.txt', 'file15.txt', 'file16.txt', 'file17.txt', 'file18.txt', 'file19.txt', 'file2.txt', 'file20.txt', 'file21.txt', 'file22.txt', 'file23.txt', 'file24.txt', 'file25.txt', 'file3.txt', 'file4.txt', 'file5.txt', 'file6.txt', 'file7.txt', 'file8.txt', 'file9.txt']

Then use a list comprehension with regex to narrow that list:

def expand(x,lo=0,hi=sys.maxint): 
    return lo<=int(re.search(r'\d+', x).group(0))<=hi

print [e for e in glob.glob('file[0-9]*.txt') if expand(e, 8,12)]
# ['file10.txt', 'file11.txt', 'file12.txt', 'file8.txt', 'file9.txt']

Or use filter:

print filter(lambda x: expand(x, 9, 12), glob.glob('file[0-9]*.txt'))
# ['file10.txt', 'file11.txt', 'file12.txt', 'file9.txt']

what are you looking for?

there is always range(19,22) which is depending on what you are looking close to curly expansion

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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