Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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__

share|improve this question
This should be in . – Ramchandra Apte Sep 30 '13 at 7:17
up vote 1 down vote accepted

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])
share|improve this answer
thank you very much... :) – Naive Sep 30 '13 at 8:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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