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 8281751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:08:31+00:00 2026-06-08T10:08:31+00:00

im rubbish with regex if someone could help id be very appreciative. its going

  • 0

im rubbish with regex if someone could help id be very appreciative.

its going to be a bit of a tough one i imagine – so my hats off too anyone that can solve it!

so say we have file that contains 2 html tags in the following formats:

abc1234
<a href="http://google.com">Some Text</a> <P>
<a href="http://www.google.com" rel="nofollow">Some Text</a>
abc1234

im trying to remove everything in those tags except the url (and leaving other text) so the output of the regex in this document would be

abc1234
http://google.com <P>
http://www.google.com
abc1234

Can any guru figure this one out? Id prefer one regex expression to handle both cases but two seperate ones would be fine too.

Thanks in advance/

  • 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-08T10:08:34+00:00Added an answer on June 8, 2026 at 10:08 am

    I’m a Rubyist, so my example is going to be in Ruby. I’d recommend using two regexes, just to keep things straight:

    url_reg = /<a href="(.*?)"/   # Matches first string within <a href=""> tag
    tag_reg = /(<a href=.*?a>)/   # Matches entire <a href>...</a> tag
    

    You’ll want to pull the URL with the first regex out and store it temporarily, then replace the entire contents of the tag (matched with the tag_reg) with the stored URL.

    You might be able to combine it, but it doesn’t seem like a good idea. You’re fundamentally altering (by deleting) the original tag, and replacing it with something inside itself. Less chance of things going wrong if you separate those two steps as much as possible.

    Example in Ruby

    def replace_tag(input)
      url_reg = /<a href="(.*?)"/    # Match URLS within an <a href> tag
      tag_reg = /(<a href=.*?a>)/     # Match an entire <a href></a> tag
    
      while (input =~ tag_reg) # While the input has matching <a href> tags
        url = input.scan(url_reg).flatten[0]  # Retrieve the first URL match
        input = input.sub(tag_reg, url)       # Replace first tag contents with URL
      end
    
      return input
    end
    
    File.open("test.html", "r") do |html_input|       # Open original HTML file
      File.open("output.html", "w") do |html_output|  # Open an output file
        while line = html_input.gets                  # Read each line
          output = replace_tag(line)                  # Perform necessary substitutions
          html_output.puts(output)                    # Write output lines to file
        end
      end
    end
    

    Even if you don’t use Ruby, I hope the example makes sense. I tested this on your given input file, and it produces the expected output.

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

Sidebar

Related Questions

I'm rubbish at regular expressions, so would someone be able to help me find:
I have written a script. It does work! It's just a bit rubbish. Could
Okay, have a bit of a tricky one (for me anyway, i'm pretty rubbish
Its reading the first 3 address's right and the last one wrong...Ignore the rubbish
looks like today is going to be another rubbish one. we have recently updated
I'm a bit rubbish with Regex and was wondering if some kind person would
Can someone offer me some advice on TeamCity+CI+Deployment So far my it feels rubbish
I'm an iPhone Developer mainly, I'm a bit rubbish at CSS and I'm trying
im total rubbish at maths, and its hurting my head trying to figure this
I realise that IE is a bit rubbish at most things but I was

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.