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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:28:25+00:00 2026-06-04T11:28:25+00:00

trying to get a regex that will match a url e.g. ‘http://www.test.com’ and then

  • 0

trying to get a regex that will match a url e.g. ‘http://www.test.com’ and then going to put anchor tags around it – that part is working already with following:

regex = @"(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:;,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:;,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:;,.]*\)|[A-Z0-9+&@#\/%=~_|$])"
msg = r.Replace( msg, "<a target=\"_blank\" href=\"$0\">$0</a>" );

but when there are image tags in the input text it incorrectly puts anchor tags inside the image tag’s src attribute e.g.

<img src="<a>...</a>" />;

so far I’m trying this to bypass that: (not working)

regex = @"(?!(src=""))(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:;,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:;,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:;,.]*\)|[A-Z0-9+&@#\/%=~_|$])"

EDIT:

(example testing input):

<p>
    www.test1.com<br />
    <br />
    http://www.test2.com<br />
    <br />
    https://www.test3.com<br />
    <br />
    &quot;https://www.test4.com<br />
    <br />
    &#39;https://www.test4.com<br />
    <br />
    =&quot;https://www.test4.com</p>
<p>
    &nbsp;</p>
<p>
    <img alt="" src="..." style="width: 500px; height: 375px;" /></p>

(example output):

<p>
    <a target="_blank" href="www.test1.com">www.test1.com</a><br />
    <br />
    <a target="_blank" href="http://www.test2.com">http://www.test2.com</a><br />
    <br />
    <a target="_blank" href="https://www.test3.com">https://www.test3.com</a><br />
    <br />
    &quot;<a target="_blank" href="https://www.test4.com">https://www.test4.com</a><br />
    <br />
    &#39;<a target="_blank" href="https://www.test4.com">https://www.test4.com</a><br />
    <br />
    =&quot;<a target="_blank" href="https://www.test4.com">https://www.test4.com</a></p>
<p>
    &nbsp;</p>
<p>
    <img alt="" src="<a target="_blank" href="...">...</a>" style="width: 500px; height: 375px;" /></p>

(desired output ):

<p>
    <a target="_blank" href="www.test1.com">www.test1.com</a><br />
    <br />
    <a target="_blank" href="http://www.test2.com">http://www.test2.com</a><br />
    <br />
    <a target="_blank" href="https://www.test3.com">https://www.test3.com</a><br />
    <br />
    &quot;<a target="_blank" href="https://www.test4.com">https://www.test4.com</a><br />
    <br />
    &#39;<a target="_blank" href="https://www.test4.com">https://www.test4.com</a><br />
    <br />
    =&quot;<a target="_blank" href="https://www.test4.com">https://www.test4.com</a></p>
<p>
    &nbsp;</p>
<p>
    <img alt="" src="..." style="width: 500px; height: 375px;" /></p>
  • 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-04T11:28:26+00:00Added an answer on June 4, 2026 at 11:28 am

    Here’s the regex that solved the issue for me:

    String regex = @"(?<!(""|'))((http|https|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:;,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:;,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:;,.]*\)|[A-Z0-9+&@#\/%=~_|$])";
    

    I used a lookback negative assertion to make sure that the url doesn’t have an opening quote before it

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

Sidebar

Related Questions

I'm trying to get a regex that will match: somefile_1.txt somefile_2.txt somefile_{anything}.txt but not
I'm stuck trying to get a regex to match a filetype in a sorting
I'm trying to write a regex function that will identify and replace a single
Hi I am trying to create a regex that will do the following grab
I'm trying to get the following regex to work on my String: Pattern Regex
I'm trying to get all words inside a string using Boost::regex in C++. Here's
I am a bit puzzled with my Regex results (and still trying to get
I am trying to write a regex to get the numbers from strings like
I am trying to write out a regex to get each insert line from
I'm trying to parse phone number with regex. Exactly I want to get a

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.