我使用Python来匹配列表(数组),但我确定问题在于正则表达式本身。正则表达式不匹配第一个结果
假设我有以下几点:
foo.html
bar.html
teep.html
而且我用下面的正则表达式:.*(?=.html)
其中.*
将匹配任何东西,(?=.html)
需要串存在,但不包括它的结果
因此,我应该只留下以前的东西.html
当我检查,它只匹配数组中的第一项(在这种情况下foo
),但为什么不是其他
my_regex = re.compile('.html$')
r2 = re.compile('.*(?=.html)')
start = '/path/to/folder'
os.chdir(start)
a = os.listdir(start)
for item in a:
if my_regex.search(item) != None and os.path.isdir(item):
print 'FOLDER MATCH: '+ item # this is a folder and not a file
starterPath = os.path.abspath(item)
outer_file = starterPath + '/index.html'
outer_js = starterPath + '/outliner.js'
if r2.match(item) != None:
filename = r2.match(item).group() # should give me evertying before .html
makePage(outer_file, outer_js, filename) # self defined function
else:
print item + ': no'
请显示代码。 –
@WiktorStribiżew加了 – Kervvv
好吧,'r'。*(?= \。html)'对你来说是一个更好的正则表达式,但是主要的问题不在于正则表达式。 –