ibr2 ibr2 - 16 days ago 5
Python Question

regex issues file extension Python 2.7

import re, urllib

def get_files(page):
a = urllib.urlopen(page)
b = a.read()
c = re.findall("([a-zA-Z0-9]+\.{1}(jpg|bmp|docx|gif))",b)
return c
def main():
print get_files("http://www.soc.napier.ac.uk/~40001507/CSN08115/cw_webpage/index.html")

if __name__ == "__main__":
main()


After I ran this code, I had an issue with its regex, hence the answer will be like this:

[('clown.gif', 'gif'), ('sleeper.jpg', 'jpg'), ('StarWarsReview.docx', 'docx'), ('wargames.jpg', 'jpg'), ('nothingtoseehere.docx', 'docx'), ('starwars.jpg', 'jpg'), ('logo.jpg', 'jpg'), ('certified.jpg', 'jpg'), ('clown.gif', 'gif'), ('essays.gif', 'gif'), ('big.jpg', 'jpg'), ('Doc100.docx', 'docx'), ('FavRomComs.docx', 'docx'), ('python.bmp', 'bmp'), ('dingbat.jpg', 'jpg')]


I don't want the result to be like this
('clown.gif', 'gif')
all I want it to be like is
['clown.gif','sleeper.jpg']
and so on

Is there anyway to do it?? and get red of the tuple??

Answer

You just need to turn your group into a non-capturing group.

def get_files(page):
    a = urllib.urlopen(page)
    b = a.read()
    c = re.findall("([a-zA-Z0-9]+\.{1}(?:jpg|bmp|docx|gif))", b)