Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8851477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:09:53+00:00 2026-06-14T13:09:53+00:00

I am trying to find a link which contains http or // or \

  • 0

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()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:09:54+00:00Added an answer on June 14, 2026 at 1:09 pm

    You might want to use regular expressions for this:

    line = re.sub(r'(http://[^\s]+)', r'<a href>\1</a>', line)
    

    That just handles the http:// case. To handle all three, just do this:

    line = re.sub(r'(http://[^\s]+|//[^\s]+|\\\\[^\s]+)', r'<a href>\1</a>', line)
    

    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:

    <a href>http://foo/bar</a>
    

    … but this:

    <a href="http://foo/bar">http://foo/bar</a>
    

    To get that, just change the sub replacement expression to r'<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:

    index = 0
    while index is not None:
        index = min(line.find(pattern, index) for pattern in ('http:', '//', '\\\\'))
        if index == -1:
            break
        space = line.find(' ', index)
        if space == -1:
            space = None
        line = line[:index] + '<a href>' + line[index:space] + '</a>' + line[space:]
        index = space
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to find all href links on a webpage and replace the link
I am trying to find the first link of my page which url fit
http://54.251.60.177/StudentWebService/StudentDetail.asmx The above link is the web service, which contains two methods namely GetStudentDetails
I was trying a problem on SPOJ ,in which we have to simply find
I am trying to write a GreaseMonkey script in which I want to find
I am trying to find the text and if the text is found then
I have a cart which contains items, in my controller index method I use
I'm trying to find a way to link an array controller to an Array.
I'm trying to find a way to display one link to an IE user
I have a table which repeats an image link to a file download for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.