Following code is to extract /support/security/*.html links from a file(urlfile contain about 1000 links) to urlsort file using regex,But i'm weak in regex can anyone show me how to do that...?

#!/usr/bin/env python
import re,sys

fileHandle = open('urlfile', 'r')
f1 = open('urlsort', 'w')
for line in fileHandle.readlines():

    links = re.findall(r"(\/support\/security\/*.html.*?)", line)
    for link in links:
        sys.stdout = f1
        print ('%s' % (link[0]))
        sys.stdout = sys.__stdout__

  • This should be in stackoverflow.com . – Ramchandra Apte Sep 30 '13 at 7:17

Your regex has two mistakes, a missing . before the first * and an extra ? near the end.

Here is some code that writes urls matching your pattern to urlsort using some python idioms.

#!/usr/bin/env python

import re

with open('urlfile', 'r') as urls_in:
    with open('urlsort', 'w') as urls_out:
        for line in urls_in:
            links = re.findall(r"(\/support\/security\/bulletins\/.*.html)", line)
            if links:
                urls_out.write("%s\n" % links[0])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.