I am trying to find a link which contains http or // or \ and surround with a href tag once its found,does anyone have any ideas on how this can be done
INput:-http://pastebin.com/p9H8GQt4
sanity_results = sanity_results.replace('\n','<br>\n')
return sanity_results
def main ():
resultslis=[]
xmlfile = open('results.xml','r')
contents = xmlfile.read()
testresults=getsanityresults(contents)
#print testresults
for line in testresults:
#print line
line = line.strip()
#print line
line = re.sub(r'(http://[^\s]+|//[^\s]+|\\\\[^\s]+)', r'<a href="\1">\1</a>', line)
print line
resultslis.append(line)
print resultslis
if __name__ == '__main__':
main()
You might want to use regular expressions for this:
That just handles the
http://case. To handle all three, just do this:Play with that regexp in the console to make sure it does what you want, but it seems to do what you asked for with your posted input data. As I mentioned in a comment, in general, you need to figure out what delimiters can end a link if you want to auto-link text.
Meanwhile, are you sure the problem spec is correct? Normally, you don’t want this:
… but this:
To get that, just change the
subreplacement expression tor'<a href="\1">\1</a>'.You could also write the whole thing with string functions, but for anything but simple cases, that actually turns out to be a lot harder than learning regexps. For example, the equivalent of the above one-liner is something like this:
Except that I’m willing to bet I’ve got at least one obvious fencepost error in there, and likely at least one subtle bug with possible overlapping patterns, and so on.