2017-04-17 108 views
0

我使用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' 
+0

请显示代码。 –

+0

@WiktorStribiżew加了 – Kervvv

+0

好吧,'r'。*(?= \。html)'对你来说是一个更好的正则表达式,但是主要的问题不在于正则表达式。 –

回答

1
filename = r2.match(item).group() 

应该

filename = r2.match(item).groups() # plural ! 

根据文档,group将返回一个或多个子组,而groups将返回全部。

+0

查看我的回复更新。 '.groups()'不幸的工作,但事实证明它不会导致问题。只是一个简单的菜鸟开发者的错误。 – Kervvv

1

找出问题所在。在我的功能中,我改变了目录,但从未改变过。所以当函数结束并返回for循环时,它现在正在错误位置查找文件夹名称。它是那样简单

def makePage(arg1, arg2, arg3): 
    os.chdir('path/to/desktop') 
    # write file to new location 
    os.chdir(start) # go back to start and continue original search 
    return 

而且.group()工作对我来说,回到文件夹名称都串.html之前而.groups()刚回到()

在原来的职位代码保持不变。东西这么简单,造成这一切头痛..

相关问题