I'm newbie in Python, but this question is not a homework (actually this code helps to generate RSS on my Subversion server).
I have an array of strings in the info_lines
variable. And I want to replace each occurrence of the bug ID. My current code looks like the following:
for ln in range(3, len(info_lines)): # skip two strings since there are author&date info
if re.search( r'(?:BUG|FIX):(?:[ ,]*(\d+))+', info_lines[ln] ):
info_lines[ln] = re.sub( r'(\d+)+', r'<a href="http://bugzilla.mycompany.com/show_bug.cgi?id=\1">\1</a>', info_lines[ln] )
formatted_lines = "<br/>".join( info_lines[3:] )
It should replace the following text:
STABLE
FIX: some bug fixed
FIX: 10, 24, 3355
FIX: error 1024 was fixed
with this one:
STABLE
FIX: some bug fixed
FIX: <a href="http://bugzilla.mycompany.com/show_bug.cgi?id=10">10</a>, <a href="http://bugzilla.mycompany.com/show_bug.cgi?id=24">24</a>, <a href="http://bugzilla.mycompany.com/show_bug.cgi?id=3355">3355</a>
FIX: error 1024 was fixed
Notice that 1024 shouldn't be replaced with the link.
My current code do the job, but I interested whether it could be simplified, optimized, etc. May be it could be replaced with only one replacement regular expression? Or it could be replaced with one magic Python function from known libraries?