Kervvv
Kervvv

Reputation: 845

Regex Doesn't Match Beyond 1st Result

I'm using Python to match against a list (array), but I'm sure the problem lies on the regex itself.

Assuming I have the following:

foo.html
bar.html
teep.html

And I use the following regex: .*(?=.html)

where .* will match anything and (?=.html) requires the string be present, but does not include it in the results

Thus, I should just be left with what's before .html

When I check, it only matches the first item in the array (in this case foo), but why not the others

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'

Upvotes: 0

Views: 59

Answers (2)

Kervvv
Kervvv

Reputation: 845

Figured out the problem. In my function, I changed directories, but never changed back. So when function ended and went back to for loop, it was now looking for the folder name in the wrong location. It's as simple as

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

Also .group() worked for me and returned everything in the folder name before the string .html whereas .groups() just returned ()

The code in original post stayed the same. Something so simple, causing all this headache..

Upvotes: 1

B. Barbieri
B. Barbieri

Reputation: 1139

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

should be

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

According to the documentation, group will return one or more subgroups, whereas groups will return them all.

Upvotes: 1

Related Questions